Python- Strptime Valueerror Unconverted Data Remains: :00

Python- Strptime Valueerror Unconverted Data Remains: :00

I have a .csv file with a column that have dates which looks like "2/15/2016 1:44:00 PM",am running into following error with the code below...can anyone provide inputs on what is wrong?

CODE:-

import csv
import datetime as dt
import os

File = 'C:/Users/Alan Cedeno/Desktop/Test_Folder/HiSAM1_data_160215_164858.csv'
root, ext = os.path.splitext(File)
output = root + '-new.csv'
with open(File,'r') as csvinput,open(output, 'w') as csvoutput:

    writer = csv.writer(csvoutput, lineterminator='\n')
    reader = csv.reader(csvinput)

    all = []
    row = next(reader)
    for line in reader:
        row.append(dt.datetime.strptime(line[0],'%m/%d/%Y %H:%M').time())
        all.append(row)

    for row in reader:
        row.append(row[0])
        all.append(row)

    writer.writerows(all)

ERROR -

ValueError                                Traceback (most recent call last)
c:\users\alance~1\appdata\local\temp\tmpujjfa2.py in <module>()
     14         row = next(reader)
     15         for line in reader:
---> 16             row.append(dt.datetime.strptime(line[0],'%m/%d/%Y %H:%M'))
     17             all.append(row)
     18 

C:\Users\Alan Cedeno\AppData\Local\Enthought\Canopy\App\appdata\canopy-   1.6.1.3253.win-x86_64\lib\_strptime.pyc in _strptime(data_string, format)
    326     if len(data_string) != found.end():
    327         raise ValueError("unconverted data remains: %s" %
--> 328                           data_string[found.end():])
    329 
    330     year = None

ValueError: unconverted data remains: :00 

Here is a screenshot of the Data-

1 Answer

According to the information in the question and the traceback given in comments, your datetime format string is incomplete - missing seconds, should be:

%m/%d/%Y %H:%M:%S
5

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

James H. Sterling
Author

James H. Sterling

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.