这是我的python代码:
from itertools import groupby
from operator import itemgetter
from datetime import datetime
import locale
o = open('F6.txt','w')
i = 0
count = 0
try:
reviews = []
tosort = []
with open('fileaa','rb') as f:
for line in f:
line.strip()
line2 = line.split('\t')
if(len(line2) == 8):
reviews.append(line2)
else:
count+=1
print len(reviews)
reviews.sort(key=itemgetter(1))
for key, group in groupby(reviews, key=itemgetter(1)):
i = 0
tosort = []
for value in group:
tosort.append(value)
tosort.sort(key = lambda x: datetime.strptime(x[2],'%B %d, %Y'))
#tosort.sort(key = lambda x: x[2].split(' '))
for line in tosort:
i+=1
o.write(str(i))
o.write('\t')
o.write(line[7])
except Exception as e:
print e
print "line =", line
raise
我收到此错误:
ValueError: time data '' does not match format '%B %d, %Y'
在此行上有日期August 3, 2001
(我打印了发生错误的行)。此日期的格式正确。为什么我收到此错误?