0

I am using Python 2.7.3 and trying to parse a CSV file using csv reader as:

   date_format = '%m/%d/%Y %H:%M%'
   with open(data_base+data_file_short, 'rb') as f:
    reader = csv.reader(f)
    for row in reader:
     #use row here to convert string to date
     dateObj1 = datetime.strptime(row[1],date_format) 

But I keep getting this error:

ValueError: time data 'PostCreationDate' does not match format '%m/%d/%Y %H:%M'

If i print date it shows 8/3/2012 21:46 which seems to fit with my date_format string.

1 Answer 1

1

You're trying to interpret the header row of your CSV as a data row, which (as you can see) will not work.

Consider using a csv.DictReader instead so you can automatically handle the header row, and don't have to hardcode the row indices.

Sign up to request clarification or add additional context in comments.

2 Comments

Spot on! Thanks a lot, for some reason I just could not think about first row!
@Moon: you could skip the first line (header) using next(f).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.