我有一个数据文件,文件中的第二列是格式为'01 / 01/2007'的日期。我试图将此列转换为数字格式,以便我可以将文本文件中的数据插入到mysql数据库中。当我尝试这样做时,我不断收到这些错误:
Traceback (most recent call last):
File "C:/Python27/numpy", line 5, in <module>
x = np.loadtxt(fname='xyz.txt', dtype=[('date', 'str', 12),('x','float')], converters={1:datestr2num}, delimiter=None, skiprows=0, usecols=None);
File "C:\Python27\lib\site-packages\numpy\lib\npyio.py", line 713, in loadtxt
X.append(tuple([conv(val) for (conv, val) in zip(converters, vals)]))
File "C:/Python27/numpy", line 4, in datestr2num
return datetime.datetime.strptime(s,'"%m/%d/%y"')
File "C:\Python27\lib\_strptime.py", line 325, in _strptime
(data_string, format))
ValueError: time data '"01/01/2007"' does not match format '"%m/%d/%y"'
任何人都可以帮我这个吗?这是我正在尝试的代码:
import numpy as np
import datetime
def datestr2num(s):
return datetime.datetime.strptime(s,'"%m/%d/%y"')
x = np.loadtxt(fname='xyz.txt', dtype= 'float', converters={1:datestr2num}, delimiter=None, skiprows=0, usecols=None);
print x;