ValueError: unconverted data remains: 02:05

160,140

Solution 1

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.

Solution 2

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

Solution 3

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.

Solution 4

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",

Solution 5

  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.

Share:
160,140
4m1nh4j1
Author by

4m1nh4j1

This is not a bio.

Updated on July 09, 2022

Comments

  • 4m1nh4j1
    4m1nh4j1 almost 2 years

    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 ?

  • Martijn Pieters
    Martijn Pieters over 10 years
    You are missing the : there. You are also missing out on the richness of the datetime object that you parsed out by trying to compare it to strings.
  • 4m1nh4j1
    4m1nh4j1 over 10 years
    Can you please explain how can I use the richness of datetime in this context? Thanks anyway .
  • Martijn Pieters
    Martijn Pieters over 10 years
    See my answer, where I use st.date() == date.today() to test if the parsed value is today instead.
  • demongolem
    demongolem over 6 years
    I think that if %z were part of that timestamp, then the alternative would be necessary because the timezone is not handled.
  • Adam Starrh
    Adam Starrh over 5 years
    I got the same error when I had swapped the month and date variables by accident. It was showing unconverted data remains: 3. I thought 3 was a unit, but it was simply the part of the string leftover after the parser had finished.
  • funnydman
    funnydman over 4 years
    Sometimes it's can be really useful, but keep in mind that this solution usually works several times slower.
  • Kusal Hettiarachchi
    Kusal Hettiarachchi over 3 years
    I have the same error parsing "09 Dec 2020 11:33:28 AM" with the format "%d %b %Y %I:%M:%S %p". baffles me.
  • Malek Kamoua
    Malek Kamoua about 2 years
    For me it was unconverted data remains: 8 and it was about the milliseconds ! This comment's section helped me figure out the problem. Thank you !