加载带有日期时间的txt

早上好,我对Python真的很陌生,我正在尝试将数据时间加载到Python中。我找到了各种选项,但没有一个能够正确解决问题。我看到这段代码,但它显然不起作用,“因为 strptime () 恰好需要 2 个参数(给定 1 个)”


from datetime import datetime

data2012 = numpy.loadtxt("C:\\Users\\...\\onde_corr_2012.txt", converters={0:datetime.strptime('%m/%d/%y  %H:%M:%S')}, delimiter=',')

txt 文件的结构如下:


01-01-2012 00:00,0.36,-3.42,0.76

01-01-2012 00:30,0.35,-3.45,0.75

01-01-2012 01:00,0.35,-3.42,0.76

01-01-2012 01:30,0.36,-3.43,0.76

01-01-2012 02:00,0.36,-3.44,0.75

...


翻阅古今
浏览 92回答 1
1回答

噜噜哒

尝试:from datetime import datetimedata2012 = np.loadtxt("try.txt", converters={0:lambda t: datetime.strptime(t.decode(), '%m-%d-%Y %H:%M')}, delimiter=','&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , dtype={&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'names': ('time', 'col1', 'col2', 'col3',),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'formats': ('datetime64[m]', 'float', 'float', 'float')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;)data2012array([('2012-01-01T00:00', 0.36, -3.42, 0.76),&nbsp; &nbsp; &nbsp; &nbsp;('2012-01-01T00:30', 0.35, -3.45, 0.75),&nbsp; &nbsp; &nbsp; &nbsp;('2012-01-01T01:00', 0.35, -3.42, 0.76),&nbsp; &nbsp; &nbsp; &nbsp;('2012-01-01T01:30', 0.36, -3.43, 0.76),&nbsp; &nbsp; &nbsp; &nbsp;('2012-01-01T02:00', 0.36, -3.44, 0.75)],&nbsp; &nbsp; &nbsp; dtype=[('time', '<M8[m]'), ('col1', '<f8'), ('col2', '<f8'), ('col3', '<f8')])或者:df = pd.read_csv("try.txt", header=None, date_parser=[0])df&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0&nbsp; &nbsp; &nbsp;1&nbsp; &nbsp; &nbsp;2&nbsp; &nbsp; &nbsp;30&nbsp; 01-01-2012 00:00&nbsp; 0.36 -3.42&nbsp; 0.761&nbsp; 01-01-2012 00:30&nbsp; 0.35 -3.45&nbsp; 0.752&nbsp; 01-01-2012 01:00&nbsp; 0.35 -3.42&nbsp; 0.763&nbsp; 01-01-2012 01:30&nbsp; 0.36 -3.43&nbsp; 0.764&nbsp; 01-01-2012 02:00&nbsp; 0.36 -3.44&nbsp; 0.75
打开App,查看更多内容
随时随地看视频慕课网APP