我在 json 文件中有一些日期,我正在寻找与今天日期相对应的日期:
import os
import time
from datetime import datetime
from pytz import timezone
input_file = file(FILE, "r")
j = json.loads(input_file.read().decode("utf-8-sig"))
os.environ['TZ'] = 'CET'
for item in j:
lt = time.strftime('%A %d %B')
st = item['start']
st = datetime.strptime(st, '%A %d %B')
if st == lt :
item['start'] = datetime.strptime(st,'%H:%M')
我遇到了这样的错误:
File "/home/--/--/--/app/route.py", line 35, in file.py
st = datetime.strptime(st, '%A %d %B')
File "/usr/lib/python2.7/_strptime.py", line 328, in _strptime
data_string[found.end():])
ValueError: unconverted data remains: 02:05
你有什么建议吗?
最佳答案
st
在 st = datetime.strptime(st, '%A %d %B')
行的值类似于 01 01 2013 02: 05
和 strptime
无法解析这个。实际上,除了日期之外,您还有一个小时...您需要在 strptime 添加 %H:%M
。
关于python - 值错误 : unconverted data remains: 02:05,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20327937/