防止datetime.strptime在格式不匹配的情况下退出 [英] Prevent datetime.strptime from exit in case of format mismatch

查看:183
本文介绍了防止datetime.strptime在格式不匹配的情况下退出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在解析测量文件中的日期(大约20万行)。格式是日期和度量。日期格式为 2013-08-07-20-46或时间格式为%Y-%m-%d-%H-%M。时间戳经常是不好的角色。 (数据来自具有中断的串行链接)。条目看起来像是:201-08-11-05-15。

I am parsing dates from a measurement file (about 200k lines). The format is a date and a measurement. The date format is "2013-08-07-20-46" or in time format "%Y-%m-%d-%H-%M". Ever so often the time stamp has a bad character. (The data came from a serial link which had interruptions). The entry would look like : 201-08-11-05-15 .

我将时间字符串转换为秒的解析行是:

My parsing line to convert the time string into seconds is :

time.mktime(datetime.datetime.strptime(dt, "%Y-%m-%d-%H-%M").timetuple())

我在线上获取它,但并不完全了解它的工作原理。 (但它可以工作)。

I got it online and don't fully understand how it work. (But it works)

我的问题是防止程序在格式不匹配时抛出错误退出。有没有一种方法可以防止strptime没有退出,但是可以优雅地返回一个错误标志,在这种情况下,我将简单地丢弃数据行并移至下一条。是的,我可以使用regexp进行模式检查,但是我想知道strptime中是否已经内置了一些智能的不匹配处理。

My problem is to prevent the program from throwing error exit when a format mismatch happens. Is there a way to prevent the strptime to no exit but gracefully return an error flag in which case I would simple discard the data line and move on to the next. Yes, I can perform a pattern check with regexp but I was wondering if some smart mismatch handling is already built into strptime.

在@ Anand S Kumar上追加

Append @ Anand S Kumar

它工作了几行,但随后失败了。

It worked for a few bad lines but then it failed.

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
>>> 

追加@ Anand S Kumar

Append @ Anand S Kumar

再次崩溃。

for line in fp:
    print line
    dt,t,p = line.split(' ',2)
    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

失败:

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()
>>> 


推荐答案

您可以使用 try..except 捕获任何 ValueError ,如果发生任何此类值错误,请移至下一行。-p

You can use try..except catching any ValueError and if any such value error occurs, move onto the next line. Example -

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

如果您正在做其他事情(例如每行的函数调用,则在中返回None左右,除了块)

If you are doing something else (maybe like a function call for each line , then return None or so in the except block)

第二个 ValueError 您应该在行中出现-

The second ValueError you are getting should be occuring in line -

[dt,t,p]= string.split(line)

之所以会出现此问题,是因为可能有一个特定的行导致了3个以上的元素。为此,您可以做的一件事是使用 str.split()中的 maxspplit 参数次。示例-

This issue is occur because there maybe a particular line that is resulting in more than 3 elements. One thing you can do for this would be to use the maxspplit argument from str.split() to split maximum 3 times. Example -

dt,t,p = line.split(None,2)

或者如果您真的要使用 string.split()-

Or if you really want to use string.split() -

[dt,t,p]= string.split(line,None,2)

或者,如果您不希望在任何字段内使用空格,则可以在包含 try..except 块中的code> ValueError 并将其视为错误行。

Or if you are not expecting space inside any of the fields, you can include the line causing the ValueError inside the try..except block and treat it as a bad line.

这篇关于防止datetime.strptime在格式不匹配的情况下退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆