Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
497 views
in Technique[技术] by (71.8m points)

python - ValueError: unconverted data remains: 02:05

I have some dates in a json files, and I am searching for those who corresponds to today's date :

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') 

I had an error like this :

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

Do you have any suggestions ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The value of st at st = datetime.strptime(st, '%A %d %B') line something like 01 01 2013 02:05 and the strptime can't parse this. Indeed, you get an hour in addition of the date... You need to add %H:%M at your strptime.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...