我有一个.csv文件,其列的日期看起来像" 2/15/2016 1:44:00 PM",我遇到以下错误,代码如下...可以有谁提供了什么错误的输入?
代码: -
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)
错误 -
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
以下是数据的屏幕截图 -
答案 0 :(得分:1)
根据问题中的信息和评论中给出的回溯,您的日期时间格式字符串不完整 - 缺少秒数,应为:
%m/%d/%Y %H:%M:%S