time_list 是一个元组列表,每个元组内是一个整数和两个字符串,这两个字符串是日期格式,但我想将它们更改为实际的日期时间对象。这就是我所做的:
for row in time_list:
row[1] = datetime.strptime(row[1], "%Y-%m-%d %H:%M:%S.%5N")
row[2] = datetime.strptime(row[2], "%Y-%m-%d %H:%M:%S.%5N")
delta = row[2] - row[1]
timedelta.append(delta)
当我删除 %5N 时,我得到一个不同的错误,但这不是我想要做的,所以我不会为此烦恼。我确实知道 %5N 导致了这个问题。我得到的错误是:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\user\Programs\time_database.py", line 50, in search2
row[1] = datetime.strptime(row[1], "%Y-%m-%d%t%H:%M:%S.%5N")
File "C:\Python27\lib\_strptime.py", line 317, in _strptime
(bad_directive, format))
ValueError: '5' is a bad directive in format '%Y-%m-%d %H:%M:%S.%5N'