gpt4 book ai didi

python - 比较日期的最简单方法是什么?

转载 作者:行者123 更新时间:2023-12-01 08:05:45 25 4
gpt4 key购买 nike

所以我有一个具有以下格式的文本文件:

[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)是在当前日期之前还是之后。如果我发现日期时间大于给定日期的行,我就知道我需要从该行搜索字符串。

所以我的问题是如何比较两个日期时间

最佳答案

由于 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)

For Python >= 3.7.0, you could use datetime.fromisoformat() instead of datetime.strptime().

date = datetime.fromisoformat(date)

关于python - 比较日期的最简单方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55546039/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com