Python 防止格式不匹配时退出datetime.strTime
我正在解析一个度量文件中的日期(大约200k行)。格式为日期和度量值。日期格式为“2013-08-07-20-46”或时间格式为“%Y-%m-%d-%H-%m”。时间戳常常有一个坏的特征。(数据来自中断的串行链路)。条目看起来像:201-08-11-05-15 将时间字符串转换为秒的解析行是:Python 防止格式不匹配时退出datetime.strTime,python,datetime,Python,Datetime,我正在解析一个度量文件中的日期(大约200k行)。格式为日期和度量值。日期格式为“2013-08-07-20-46”或时间格式为“%Y-%m-%d-%H-%m”。时间戳常常有一个坏的特征。(数据来自中断的串行链路)。条目看起来像:201-08-11-05-15 将时间字符串转换为秒的解析行是: time.mktime(datetime.datetime.strptime(dt, "%Y-%m-%d-%H-%M").timetuple()) 我是在网上买的,不完全了解它是如何工作的。(但它是有效
time.mktime(datetime.datetime.strptime(dt, "%Y-%m-%d-%H-%M").timetuple())
我是在网上买的,不完全了解它是如何工作的。(但它是有效的)
我的问题是防止程序在发生格式不匹配时抛出错误退出。是否有一种方法可以防止strtime不退出,但优雅地返回一个错误标志,在这种情况下,我将简单地放弃数据行并继续下一行。是的,我可以用regexp执行模式检查,但我想知道strtime中是否已经内置了一些智能的不匹配处理
附加@Anand S Kumar
它在一些糟糕的线路上工作,但后来失败了
fp = open('bmp085.dat', 'r')
for line in fp:
[dt,t,p]= string.split(line)
try:
sec= time.mktime(datetime.datetime.strptime(dt, "%Y-%m-%d-%H-%M").timetuple()) - sec0
except ValueError:
print 'Bad data : ' + line
continue #If you are doing this in a loop (looping over the lines) so that it moves onto next iteration
print sec, p ,t
t_list.append(sec)
p_list.append(p)
fp.close()
输出:
288240.0 1014.48 24.2
288540.0 1014.57 24.2
288840.0 1014.46 24.2
Bad data : �013-08-11-05-05 24.2! 1014.49
Bad data : 2013=0▒-11-05-10 �24.2 1014.57
Bad data : 201�-08-11-05-15 24.1 1014.57
Bad data : "0�#-08-1!-p5-22 24.1 1014.6
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
ValueError: too many values to unpack
>>>
失败:
2013-08-11�06-t5 03/9 9014.y
Bad data : 2013-08-11�06-t5 03/9 9014.y
2013-08-11-06-50 (23. 1014.96
295440.0 (23. 1014.96
2013-08-11-06%55 23.9 !�1015.01
Traceback (most recent call last):
File "<stdin>", line 5, in <module>
TypeError: must be string without null bytes, not str
>>> fp.close()
>>>
2013-08-11�06-t5 03/9 9014.y
坏数据:2013-08-11�06-t5 03/9 9014.y
2013-08-11-06-50 (23. 1014.96
295440.0 (23. 1014.96
2013-08-11-06%55 23.9 !�1015.01
回溯(最近一次呼叫最后一次):
文件“”,第5行,在
TypeError:必须是不带空字节的字符串,而不是str
>>>fp.close()
>>>
您可以使用捕获任何值错误
,如果出现任何此类值错误,请转到下一行。示例-
try:
time.mktime(datetime.datetime.strptime(dt, "%Y-%m-%d-%H-%M").timetuple())
except ValueError:
continue #If you are doing this in a loop (looping over the lines) so that it moves onto next iteration
dt,t,p = line.split(None,2)
如果您正在执行其他操作(可能像是对每一行进行函数调用,然后在块中返回None或so,除了
块)
您得到的第二个
ValueError
应该发生在行中-
[dt,t,p]= string.split(line)
出现此问题是因为可能有一行产生了3个以上的元素。为此,可以使用str.split()
中的maxsplit
参数最多拆分3次。示例-
try:
time.mktime(datetime.datetime.strptime(dt, "%Y-%m-%d-%H-%M").timetuple())
except ValueError:
continue #If you are doing this in a loop (looping over the lines) so that it moves onto next iteration
dt,t,p = line.split(None,2)
或者如果您真的想使用string.split()
-
[dt,t,p]= string.split(line,None,2)
或者,如果在任何字段中都不需要
空格
,则可以在try..except
块中包含导致ValueError
的行,并将其视为坏行。在for
循环中使用try-except
:
for dt in data:
try:
print time.mktime(datetime.datetime.strptime(dt, "%Y-%m-%d-%H-%M").timetuple())
except ValueError:
print "Wrong format!"
continue
数据的输出=[“1998-05-14-15-45”、“11998-05-14-15-45”、“2002-05-14-15-45”]:
try-except
是您要查找的内容。@GertGottschalk您可以检查我更新的答案中的第二个错误。@Anand S Kumar谢谢。在检查输入行时,使用regexp可能是最简单的方法(只需检查是否存在3个字符串)。我还没有决定。