我已经尝试了很多选项,并且像一些黑客一起解析一样,但我很好奇如何用strptime做到这一点?
item = "01/Jul/1995:00:00:01-0400"
checkdate = datetime.strptime(item,"%Y-%m-%dT:%H:%M%S%z")
checkdate = datetime.strptime(item,"%Y/%m/%dT:%H:%M:%S%z")
checkdate = datetime.strptime(item,"%Y-%b-%d:%H:%M%S%z")
checkdate = datetime.strptime(item,"%Y-%b-%dT:%H:%M:%S%z")
checkdate = datetime.strptime(item,"%Y/%m/%d:%H:%M:%S%z")
每次尝试得到的是:
ValueError: time data '01/Jul/1995:00:00:01-0400' does not match format '%Y/%m/%d:%H:%M:%S%z'
这是什么样的strptime格式?
编辑: 所以你是对的,我做了一个小测试
def split_date (stringdate):
datepart = []
monthDict = {'Jan':'01','Feb':'02','Mar':'03','Apr':'04','May':'05',
'Jun':'06','Jul':'07','Aug':'08','Sep':'09','Oct':'10','Nov':'11','Dec':'12'}
split1 = [part for part in stringdate.split('/')]
day = split1[0]
month = split1[1]
month = monthDict.get(month)
split2 = [part for part in split1[2].split(":")]
year = split2[0]
hour = split2[1]
minute = split2[2]
split3 = [part for part in split2[3].split('-')]
second = split3[0]
timezone = split3[1]
return datetime(int(year), int(month), int(day), int(hour), int(minute), int(second), int(timezone)
datetime_received_split = []
datetime_received_strp = []
s = time.time()
for date in data.time_received:
try:
datetime_received_split.append(split_date(date))
except:
split_fail.append(date)
e = time.time()
print ('split took {} s '.format(e-s))
s = time.time()
for date in data.time_received:
try:
datetime_received_strp.append(datetime.strptime(item,"%d/%b/%Y:%H:%M:%S- %f"))
except:
strp_fail.append(date)
e = time.time()
print ('strp took {} s'.format(e-s))
我发现手动拆分实际上更快了吗?
答案 0 :(得分:1)
Python 3.2 +支持%z。
因此,对于Python2.x,请查看How to parse dates with -0400 timezone string in python?
如果您使用的是Python3.x,可以试试这个:
from datetime import datetime
item = "01/Jul/1995:00:00:01-0400"
checkdate = datetime.strptime(item,"%d/%b/%Y:%H:%M:%S%z")
print(checkdate)
结果:
1995-07-01 00:00:01-04:00
的更多详情
答案 1 :(得分:0)
我修正了你的日期转换。有趣的是,Python 2.7和3.x都支持%f
。
from datetime import datetime
item = "01/Jul/1995:00:00:01-0400"
checkdate = datetime.strptime(item,"%d/%b/%Y:%H:%M:%S-%f")
print(checkdate)