比较文本文件中的日期并将其写入另一个文本文件python

时间:2019-03-29 17:51:19

标签: python-3.x string datetime comparison

我有一个文本文件a.txt,我想获取earlier日期起过去48小时内的所有行-20190322将其保存到文本文件中。

a.txt

     Mon Sep 17 04:00:05 2018 abc.pl:16199: snononsonfvnosnovoosr
        Mon Sep 17 04:00:05 2018 abc.pl:16199: ahelooa afoaona woom
        Fri Jan 18 04:00:05 2019 abc.pl:16199: qojfcmqcacaeia
        Sat Jan 19 04:00:05 2019 abc.pl:16199: user: number is 93823004
        user: number is 93823004
        Sun Feb 17 04:00:05 2019 abc.pl:16199: user 7fb31ecfa700
        Mon Feb 18 04:00:05 2019 abc.pl:16199: Exit Status = 0x0
        Tue Feb 19 04:00:05 2019 abc.pl:16199: std:ZMD:
        Mon Mar 18 04:00:05 2019 abc.pl:16199: snononsonfvnosnovoosr
        Tue Mar 19 04:00:05 2019 abc.pl:16199: ahelooa afoaona woom
        Wed Mar 20 04:00:05 2019 abc.pl:16199: qojfcmqcacaeia

        Thu Mar 21 04:00:05 2019 abc.pl:16199: ahelooa afoaona woom
       "udijandoa:nqHmsdddKN0WczMbHxK8Th+azfs=,domain=Device" -H 
       "jfnkoankfn" -H "auidiuayhca" -H "pakfpapf" -H "manfonancnana" -H 
       "pklamfkcm" --verbose --afpampkmfca-alkdkap 

        Tue Mar 22 04:01:59 2019 abc.pl:24549: $vca= {
                       'rc' => '',
                       'rco' => '4542',
                        };
        Fri Mar 23 04:00:05 2019 abc.pl:16199: qojfcmqcacaeia

        earlier_date = '20190322'

预期结果

result.txt:
Wed Mar 20 04:00:05 2019 abc.pl:16199: qojfcmqcacaeia

Thu Mar 21 04:00:05 2019 abc.pl:16199: ahelooa afoaona woom
"udijandoa:nqHmsdddKN0WczMbHxK8Th+azfs=,domain=Device" -H 
"jfnkoankfn" -H "auidiuayhca" -H "pakfpapf" -H "manfonancnana" -H 
"pklamfkcm" --verbose --afpampkmfca-alkdkap 

Tue Mar 22 04:01:59 2019 abc.pl:24549: $vca= {
                       'rc' => '',
                       'rco' => '4542',
                        };

我的解决方案:

from datetime import datetime, timedelta
earlier_date = '20190322'
earlier_date = datetime.strptime(earlier_date, '%Y%m%d').strftime("%b %d %H:%M:%S %Y")
cut_date = datetime.strptime(earlier_date, "%b %d %H:%M:%S %Y")  - timedelta(hours=48)
print(cut_date)
output = open('result.txt', 'w', encoding='utf-8')


def parse_file(lines_all):
    for l in lines_all:
        index = l.find(' ')
        if index > -1:
            yield l[index + 1:24], l
        else:
            pass


with open('a.txt', 'r') as f:
    lines = parse_file(f)
    dates, line = next(lines)
    print(dates)

    for dates, line in lines:
        dates = datetime.strptime(dates, "%b %d %H:%M:%S %Y")
        if cut_date <= dates:
            print(dates)
            output.write(line)

问题:它无法比较日期。因此,不会向txt文件写入任何内容。

1 个答案:

答案 0 :(得分:0)

这就是我要怎么做

from datetime import datetime, timedelta

earlier_date = "20190322"
earlier_date = datetime.strptime(earlier_date, '%Y%m%d')
cut_date = earlier_date - timedelta(hours=48)

def parse_date(line):
    date_string = " ".join(line.strip().split()[:5])
    return datetime.strptime(date_string, "%a %b %d %H:%M:%S %Y")

with open("a.txt") as infile, open("result.txt", "w") as outfile:
    for line in infile:
        if line.startswith(" ") and parse_date(line) >= cut_date:
            outfile.write(line)