我正在尝试将日期和时间转换为小数。该过程适用于另一个文件,但对于我正在使用的一组文件,我遇到了麻烦:
在“时间”列中,数据输出为:
>>> print rawtime
2016/10/18 13:31:44.079 10/18/2016 13:31:44.089 10/18/2016 13:31:44.099 10/18/2016 13:31:44.109 10/18/2016 13:31:44.119 10/18/2016 13:31:44.129 10/18/2016 13:31:44.139 10/18/2016 13:31:44.149 10/18/2016 13:31:44.159 10/18/2016 13:31:44.169 10/18/2016 13:31:44.179 10/18/2016 13:31:44.189 10/18/2016 13:31:44.199 10/18/2016 13:31:44.209 10/18/2016 13:31:44.219 ... 10/21/2016 09:16:50.968 10/21/2016 09:16:50.978 10/21/2016 09:16:50.988 10/21/2016 09:16:50.998 10/21/2016 09:16:51.008 10/21/2016 09:16:51.018 10/21/2016 09:16:51.028 10/21/2016 09:16:51.038 10/21/2016 09:16:51.048 10/21/2016 09:16:51.058 10/21/2016 09:16:51.068 10/21/2016 09:16:51.078 10/21/2016 09:16:51.088 10/21/2016 09:16:51.098 10/21/2016 09:16:51.108 名称:时间,长度:19113432,dtype:object
但是当我尝试使用strptime函数时,它表示格式错误:
>>> t = datetime.strptime(rawtime[0], '%m/%d/%y %H:%M:%S.%f')
追踪(最近一次通话): 文件“”,第1行,in 文件“C:\ Users \ bbaumgartner \ AppData \ Local \ Continuum \ Anaconda \ lib_strptime.py”,第325行,在_strptime中 (data_string,format)) ValueError:时间数据'13:31:44.079'与格式'%m /%d /%y%H不匹配:%M:%S。%f'
同样,只打印时间列会删除日期
print rawtime [0] 13:31:44.079
答案 0 :(得分:0)
看起来您忘了将" y"
大写import datetime
datetime.datetime.strptime('10/18/2016 13:31:44.079', '%m/%d/%Y %H:%M:%S.%f')
datetime.datetime(2016,10,18,13,31,44,79000)