首页 > 解决方案 > 比较日期的最简单方法是什么?

问题描述

所以我有一个这种格式的文本文件:

[2018-04-04 11:46:05.879927]c:\windows\addins\FXSEXT.ecf,[created date]2018-04-12 02:35:21,[modified date]2018-04-12 02:35:21,[access date]2018-04-12 02:35:21,[READ]True,[WRITE]True,[EXECUTED]True
[2018-04-05 15:46:15.336327]c:\windows\System32\AcGenral.dll,[created date]2018-09-23 04:14:27,[modified date]2018-08-09 12:13:19,[access date]2018-09-23 04:14:27,[READ]True,[WRITE]True,[EXECUTED]True
[2018-04-05 15:46:10.556427]c:\windows\SysWOW64\AcGenral.dll,[created date]2018-09-23 04:14:30,[modified date]2018-08-09 11:20:24,[access date]2018-09-23 04:14:30,[READ]True,[WRITE]True,[EXECUTED]True
[2018-04-05 12:46:12.596327]c:\windows\WinSxS\amd64_microsoft-windows-a..ence-mitigations-c3_31bf3856ad364e35_10.0.17134.112_none_edebf6774847bf6e\AcGenral.dll,[created date]2018-06-19 22:49:33,[modified date]2018-06-19 22:49:33,[access date]2018-06-19 22:49:33,[READ]True,[WRITE]True,[EXECUTED]True
[2019-04-02 15:46:12.596327]c:\windows\WinSxS\amd64_microsoft-windows-a..ence-mitigations-c3_31bf3856ad364e35_10.0.17134.165_none_edb8e7b9486d9728\AcGenral.dll,[created date]2018-08-06 06:10:19,[modified date]2018-07-06 16:53:16,[access date]2018-08-06 06:10:19,[READ]True,[WRITE]True,[EXECUTED]True
[2018-04-05 06:46:32.596327]c:\windows\WinSxS\amd64_microsoft-windows-a..ence-mitigations-c3_31bf3856ad364e35_10.0.17134.1_none_f1a4c5155b750465\AcGenral.dll,[created date]2018-04-12 02:34:40,[modified date]2018-06-19 22:53:10,[access date]2018-06-19 22:53:09,[READ]True,[WRITE]True,[EXECUTED]True
[2018-04-05 15:46:52.596327]c:\windows\WinSxS\amd64_microsoft-windows-a..ence-mitigations-c3_31bf3856ad364e35_10.0.17134.254_none_edc2b94148665f07\AcGenral.dll,[created date]2018-09-23 04:14:27,[modified date]2018-08-09 12:13:19,[access date]2018-09-23 04:14:27,[READ]True,[WRITE]True,[EXECUTED]True
[2018-04-01 13:46:12.798327]c:\windows\WinSxS\wow64_microsoft-windows-a..ence-mitigations-c3_31bf3856ad364e35_10.0.17134.112_none_f840a0c97ca88169\AcGenral.dll,[created date]2018-06-19 22:49:39,[modified date]2018-06-19 22:49:39,[access date]2018-06-19 22:49:39,[READ]True,[WRITE]True,[EXECUTED]True
[2018-04-05 15:46:12.431127]c:\windows\WinSxS\wow64_microsoft-windows-

我需要做的就是获取要搜索的特定日期和字符串。从这个日期开始,我需要在我的文本文件中逐行循环并返回True给定的字符串是否在文本文件中或False其他(我只需要在给定日期之后的行内搜索,文本文件已排序)

所以首先我有这个正则表达式来解析datetime每一行的开头:

date是我需要搜索的时间......

count = 0
text_file = open(file, 'r')
lines = text_file.readlines()
for line in lines:
    maches = regex.findall('\[(.*?)\]', line)
    if len(maches) > 0:
        currentdate = maches[0]
        count = count + 1
        if currentdate > date:
            # here I know  that from here all the next lines dates are after the given date.

然后我需要遍历所有行并检查给定日期(变量date)是在当前日期之前还是之后。如果我找到datetime大于给定日期的行,我知道我需要从该行中搜索字符串。

所以我的问题是如何比较 2datetime秒?

标签: pythondatetime

解决方案


由于时间戳text始终位于每行的开头,因此您可以简单地对其进行切片而不是使用正则表达式。

from datetime import datetime

file = 'text.txt'
search_date = '2018-04-04'
search_string = 'WOW'

text_file = open(file, 'r')
lines = text_file.readlines()
search_date = datetime.strptime(search_date, '%Y-%m-%d')            # Convert to datetime

for line in lines:
    date = line[1:27]                                               # Slice date from line
    date = datetime.strptime(date, '%Y-%m-%d %H:%M:%S.%f')          # Convert to datetime
    if (date > search_date) and (search_string in line):
        print(line)

对于 Python >= 3.7.0,您可以使用datetime.fromisoformat()而不是datetime.strptime().

date = datetime.fromisoformat(date)


推荐阅读