Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中将字符串时间戳转换为可比较的时间_Python_Performance_Datetime_Comparison - Fatal编程技术网

在python中将字符串时间戳转换为可比较的时间

在python中将字符串时间戳转换为可比较的时间,python,performance,datetime,comparison,Python,Performance,Datetime,Comparison,您好,我正在尝试将此格式(2/22/13 3:47:18:509 EST)的字符串时间戳转换为可比较的时间对象 问题是我有一个巨大的时间戳列表(+50000项)(按升序排列),我必须迭代和比较,但是转换列表中的每个项都很慢 我需要在一定的时间范围内(实际时间-N小时)从列表中提取项目 我怎样才能有效地做到这一点 问候 注: 以下是我将日期转换为datetime对象的方式: timestamp = datetime.datetime.strptime(_list[-1][2], "%m/%d/%

您好,我正在尝试将此格式(2/22/13 3:47:18:509 EST)的字符串时间戳转换为可比较的时间对象

问题是我有一个巨大的时间戳列表(+50000项)(按升序排列),我必须迭代和比较,但是转换列表中的每个项都很慢

我需要在一定的时间范围内(实际时间-N小时)从列表中提取项目

我怎样才能有效地做到这一点

问候

注:

以下是我将日期转换为datetime对象的方式:

timestamp = datetime.datetime.strptime(_list[-1][2],  "%m/%d/%y %H:%M:%S:%f %Z")
我得到一个错误:

ValueError: time data '3/20/13 15:12:03:762 EDT' does not match format '%m/%d/%y %H:%M:%S:%f %Z'

编辑:如果我在表达式中使用硬编码的EDT而不是%Z,它可以工作(但我需要EDT和EST)

我想你的错误是%f需要6个字符,而这不是你的主要问题。@dwxw我已经测试过了,但不起作用:ValueError:时间数据“2/22/13 10:23:28:825000 EST”与格式“%m/%d/%y%H:%m:%S:%f%Z”不匹配,奇怪的是,我刚刚试过你的例子(用
'3/20/13 15:12:03:762 EDT'
),它对我有效。我有一个剧本,但它对我无效。但是@Nucklear我看到的问题是strtime不喜欢时区'EDT',去掉它就可以了。python的
datetime
没有为时区提供实现,这是由诸如或之类的外部库提供的。对于解析时区感知的日期时间字符串,您可能需要查看