Ivin Georgi
Ivin Georgi

Reputation: 33

Why is there an error in my date format in my csv file?

from datetime import datetime
with open('directory.csv','r') as file:
    r=csv.reader(file)
    for i in r:
        if i[0]!='     NAME     ':
            data = sorted(r, key = lambda row: datetime.strptime(row[1], "%y/%m/%d"))
    print(data)

the above is my code.
and this is the output that comes:

ValueError: time data '2002/3/13' does not match format '%y/%m/%d'

Upvotes: 2

Views: 42

Answers (1)

Cory Kramer
Cory Kramer

Reputation: 117856

The Y needs to be capitalized

>>> datetime.strptime('2002/3/13', '%Y/%m/%d')
datetime.datetime(2002, 3, 13, 0, 0)

From the docs

%y Year without century as a zero-padded decimal number. (00, 01, ..., 99)

%Y Year with century as a decimal number (0001, 0002, …, 2013, 2014, …, 9998, 9999)

Upvotes: 3

Related Questions