Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.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
检查文件行sttime是否早于2天-Python_Python - Fatal编程技术网

检查文件行sttime是否早于2天-Python

检查文件行sttime是否早于2天-Python,python,Python,我在文本文件中有一系列条目,如下所示: 20150217_00:47:32 - AAAAAA 20150217_00:47:32 - BBBBBB 20150217_00:47:32 - CCCCCC 我想做一个函数,它将定期读取文件中的每一行,并根据条目的长度是小于2天、大于2天还是大于7天来执行某些操作 我不知道如何让代码理解文件中的sttime时间戳。我的代码(到目前为止)如下: with open('entries.txt', 'r+') as entries: for

我在文本文件中有一系列条目,如下所示:

20150217_00:47:32 - AAAAAA
20150217_00:47:32 - BBBBBB
20150217_00:47:32 - CCCCCC
我想做一个函数,它将定期读取文件中的每一行,并根据条目的长度是小于2天、大于2天还是大于7天来执行某些操作

我不知道如何让代码理解文件中的sttime时间戳。我的代码(到目前为止)如下:

with open('entries.txt', 'r+') as entries:
        for line in entries:
            lineitem = line.strip()
            print lineitem[:17]
这可以检索时间戳,但如何读取时间戳并将其解释为时间我不知道:/

(这将最终在执行上述功能的if循环中结束,但首先我需要知道如何读取这些时间…

这将为您提供一个对象,您可以针对其他datetime对象:

from datetime import datetime, timedelta
...
linets=datetime.strtime(“%Y%m%d\uh:%m:%s”,line.strip()[:17])

哎呀!马克错误地理解了那些论点。应该是

linets = datetime.strptime(line.strip()[:17], '%Y%m%d_%H:%M:%S')
测试

>>> print datetime.strptime('20150217_00:47:32', '%Y%m%d_%H:%M:%S')
2015-02-17 00:47:32

可能是它的重复项引发了一个错误:ValueError:时间数据“%Y%M%d\u%H:%M:%s”与格式“20150217\u 00:47:32”不匹配@joep1:很抱歉。我想Mark应该在提交代码之前测试它。我也应该注意到这一点尴尬:对不起,我后来在控制台上测试过,但也失败了:)没关系!很多人也没有注意到@真的!但作为误导你的安慰奖,我认为你应该得到一些分数。顺便说一句,你现在有足够的声望点数参加比赛。