Pergunta

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.

Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top