我面临一个小问题。我正在存储一些日期时间数据,该数据是
# "datetime","numb","temperature" "1998-04-18 16:48:36.76",0,38 "1998-04-18 16:48:36.8",1,42 "1998-04-18 16:48:36.88",2,23 "1998-04-18 16:48:36.92",3,24 "1998-04-18 16:48:36",4,42 "1998-04-18 16:48:37",5,33 "1998-04-18 16:48:37.08",6,25
日期时间列显然是字符串,所以当我尝试将其转换时,出现此错误
ValueError: time data '1998-04-18 16:48:36' does not match format '%Y-%m-%d %H:%M: %S.%f'
我的代码是
import time import datetime import calendar for k, line in enumerate(lines): if k > (int(header_line)): data_pre = line.strip().split(',') stDate = data_pre[0].replace("\"", "") print stDate # got 1998-04-18 16:48:36.76 dat_time = datetime.datetime.strptime(stDate, '%Y-%m-%d %H:%M:%S.%f') mic_sec = dat_time.microsecond timcon = calendar.timegm(dat_time.timetuple())*1000000 + mic_sec strDate = "\"" + strDate + "\"" print stDate # got "1998-04-18 16:48:36.76"
因为我的某些datetime列缺少。%f值,所以出现了此错误。我的文档可能包含数千个这样的日期时间值,因此我想出了在所有此类日期时间后附加.0的解决方案。因此,如果日期时间字符串是
"1998-04-18 16:48:36"
我的代码应附加.0以符合格式标准。例如
"1998-04-18 16:48:36.0"
我尝试将.0附加到stDate,但出现此错误
AttributeError: 'str' object has no attribute 'append'
如果有人给我一个线索,如何处理这样的问题。任何帮助将不胜感激。
更新:我浏览了您的代码,发现了一些错误类型。此外,您似乎没有添加串联。
我都整理了。
你写了:
for k, line in enumerate(lines): if k > (int(header_line)): data_pre = line.strip().split(',') stDate = data_pre[0].replace("\"", "") print stDate # got 1998-04-18 16:48:36.76 dat_time = datetime.datetime.strptime(stDate, '%Y-%m-%d %H:%M:%S.%f') mic_sec = dat_time.microsecond timcon = calendar.timegm(dat_time.timetuple())*1000000 + mic_sec strDate = "\"" + strDate + "\"" # ^ This line is wrong # It should say: # strDate = "\"" + stDate + "\"" print stDate # got "1998-04-18 16:48:36.76" # ^ This line is wrong # It should say: # print strDate
(尝试先运行此程序,在继续之前,确保您了解它在做什么):
import time import datetime import calendar A = "1998-04-18 16:48:36.76,0,38" B = "1998-04-18 16:48:37,5,33" # Run the Code for B data_pre = B.strip().split(',') print data_pre stDate = data_pre[0].replace("\"", "") print "stDate before: ", stDate ### Addition of Addition of .0 # Here, we try to convert to datetime format using the format # '%Y-%m-%d %H:%M:%S.%f' try: dat_time = datetime.datetime.strptime(stDate, '%Y-%m-%d %H:%M:%S.%f') # If that doesn't work, we add ".4" to the end of stDate # (You can change this to ".0") # We then retry to convert stDate into datetime format except: stDate = stDate + ".4" dat_time = datetime.datetime.strptime(stDate, '%Y-%m-%d %H:%M:%S.%f') print "stDate after: ", stDate ### print "dat_time: ", dat_time mic_sec = dat_time.microsecond print "mic_sec: ", mic_sec timcon = calendar.timegm(dat_time.timetuple())*1000000 + mic_sec print "timecon: ", timcon strDate = "\"" + stDate + "\"" print "strDate: ", strDate
A = "1998-04-18 16:48:36.76,0,38" B = "1998-04-18 16:48:37,5,33" # Note the difference ^^ # Output for B: ['1998-04-18 16:48:37', '5', '33'] stDate before: 1998-04-18 16:48:37 stDate after: 1998-04-18 16:48:37.4 dat_time: 1998-04-18 16:48:37.400000 mic_sec: 400000 timecon: 892918117400000 strDate: "1998-04-18 16:48:37.4" # Output for A: ['1998-04-18 16:48:36.76', '0', '38'] stDate before: 1998-04-18 16:48:36.76 dat_time: 1998-04-18 16:48:36.760000 mic_sec: 760000 timecon: 892918116760000 strDate: "1998-04-18 16:48:36.76"
for k, line in enumerate(lines): if k > (int(header_line)): data_pre = line.strip().split(',') stDate = data_pre[0].replace("\"", "") print stDate try: dat_time = datetime.datetime.strptime(stDate, '%Y-%m-%d %H:%M:%S.%f') except: stDate = stDate + ".4" dat_time = datetime.datetime.strptime(stDate, '%Y-%m-%d %H:%M:%S.%f') mic_sec = dat_time.microsecond timcon = calendar.timegm(dat_time.timetuple())*1000000 + mic_sec strDate = "\"" + stDate + "\"" # ^ Changed this line print strDate # ^ Changed this line
您不能追加到字符串。
一种选择是使用 A + B
A = "1998-04-18 16:48:36" B = ".0" C = A + B C = "1998-04-18 16:48:36.0"
您也可以使用str.join
:
D = "".join([A,B]) D = '1998-04-18 16:48:36.0'
有关更多信息,请参见以下问题的答案:在Python中连接字符串的首选方法是什么?