Nik
Nik

Reputation: 13

ValueError: unconverted data remains: 02

how must I change an element from a list that is in this form: 05/06/2020, to get a date object like: date_object = datetime.strptime(listelement, '%m/%d/%y') ?

Here is my Code:

daten = {} 

with open("Observed_Monthly_Rain_Gauge_Accumulations_-_Oct_2002_to_May_2017.csv", 'r') as csvfile: 
    regen_csv = csv.reader(csvfile)                                                               
    next(regen_csv, None)                                                                        
    for rows in regen_csv:                                                                       
        keys = rows[:1]                                                                           
        strt= str(keys[0])                                                                          in 
        date_object = datetime.strptime(strt, '%m/%d/%y')
        values = rows[1:]                                                                         
        daten[strt] = values                                                                     
        print(daten)

Traceback (most recent call last):
  File *directory*, line 16, in <module>
    date_object = datetime.strptime(strt, '%m/%d/%y')
  File "C:\Program Files\Python37\lib\_strptime.py", line 577, in _strptime_datetime
    tt, fraction, gmtoff_fraction = _strptime(data_string, format)
  File "C:\Program Files\Python37\lib\_strptime.py", line 362, in _strptime
    data_string[found.end():])
ValueError: unconverted data remains: 02

Heres a link to the list with the data: https://data.seattle.gov/api/views/rdtp-hzy3/rows.csv?accessType=DOWNLOAD

Upvotes: 0

Views: 831

Answers (1)

CDJB
CDJB

Reputation: 14506

You need to use %Y for years in the form 2020. A full list of format codes can be found here.

date_object = datetime.strptime(strt, '%m/%d/%Y')

Upvotes: 1

Related Questions