ValueError: unconverted data remains: 02:05

Question:

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 ?

Asked By: 4m1nh4j1

||

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.

Answered By: Maxime Lorant

You have to parse all of the input string, you cannot just ignore parts.

from datetime import date, datetime

for item in j:
    st = datetime.strptime(item['start'], '%A %d %B %H:%M')

    if st.date() == date.today():
        item['start'] = st.time()

Here, we compare the date to today’s date by using more datetime objects instead of trying to use strings.

The alternative is to only pass in part of the item['start'] string (splitting out just the time), but there really is no point here, not when you could just parse everything in one step first.

Answered By: Martijn Pieters

Well it was very simple. I was missing the format of the date in the json file, so I should write :

st = datetime.strptime(st, '%A %d %B %H %M')

because in the json file the date was like :

"start": "Friday 06 December 02:05",
Answered By: 4m1nh4j1

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())
Answered By: anjaneyulubatta505
  timeobj = datetime.datetime.strptime(my_time, '%Y-%m-%d %I:%M:%S')
  File "/usr/lib/python2.7/_strptime.py", line 335, in _strptime
    data_string[found.end():])
ValueError: unconverted data remains:

In my case, the problem was an extra space in the input date string. So I used strip() and it started to work.

Answered By: Vishal Kanaujia

just cut the string that match the format, do something like:

st = datetime.strptime(st[:-6], '%A %d %B')
Answered By: Ken

ValueError: unconverted data remains: 02:05 means that part of your date including time is not in the datetime.strptime used pattern my suggestion is to make simple trick and check if your string date has time or not eg. len(date_string) > 10:

from datetime import datetime

date_strings = ['2022-12-31 02:05:00', '2022-12-31', '2023-01-01 05:30:00', '2023-01-01']

dates = []
for date_string in date_strings:
    if len(date_string) > 10:
        # String has time information
        date = datetime.strptime(date_string, "%Y-%m-%d %H:%M:%S")
    else:
        # String has no time information
        date = datetime.strptime(date_string, "%Y-%m-%d")
    dates.append(date)

print(dates)

Answered By: rafalkasa
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.