Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 748
4m1nh4j1 Avatar asked Dec 02 '13 12:12

4m1nh4j1


2 Answers

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.

like image 59
Maxime Lorant Avatar answered Nov 11 '22 17:11

Maxime Lorant


Best answer is to use the from dateutil import parser.

usage:

from dateutil import parser
datetime_obj = parser.parse('2018-02-06T13:12:18.1278015Z')
print datetime_obj
# output: datetime.datetime(2018, 2, 6, 13, 12, 18, 127801, tzinfo=tzutc())
like image 24
anjaneyulubatta505 Avatar answered Nov 11 '22 16:11

anjaneyulubatta505