在python中,有没有一种方法可以从两个端点之间的日志文件打印数据

在python中,有没有一种方法可以从两个端点之间的日志文件打印数据,python,python-3.x,file-handling,Python,Python 3.x,File Handling,我有一个日志文件,正在尝试打印两个日期之间的数据 2020-01-31T20:12:38.1234Z, asdasdasdasdasdasd,...\n 2020-01-31T20:12:39.1234Z, abcdef,...\n 2020-01-31T20:12:40.1234Z, ghikjl,...\n 2020-01-31T20:12:41.1234Z, mnopqrstuv,...\n 2020-01-31T20:12:42.1234Z, wxyzdsasad,...\n impo

我有一个日志文件,正在尝试打印两个日期之间的数据

2020-01-31T20:12:38.1234Z, asdasdasdasdasdasd,...\n
2020-01-31T20:12:39.1234Z, abcdef,...\n
2020-01-31T20:12:40.1234Z, ghikjl,...\n
2020-01-31T20:12:41.1234Z, mnopqrstuv,...\n
2020-01-31T20:12:42.1234Z, wxyzdsasad,...\n
import csv

reader = csv.reader(open("logfile.log"))
filtered = filter(lambda p: p[0].split('.')[0] >= '2020-01-31T20:12:39' and p[0].split('.')[0] <= '2020-01-31T20:12:41', reader)
for l in filtered:
    print(','.join(l))
这是示例日志文件,我想打印2020-01-31T20:12:39到2020-01-31T20:12:41之间的行

到目前为止,我已经设法找到并打印了开始日期行。我已经过了开始日期

with open("logfile.log") as myFile:
    for line in myFile:
        linenum += 1
        if line.find(start) != -1:
            print("Line " + str(linenum) + ": " + line.rstrip('\n'))

但是我如何一直打印到结束日期呢?

答案不是python而是bash

sed-n'/2020-01-31T20:12:38.1234Z/,/2020-01-31T20:12:41.1234Z/p'文件。log

输出:

2020-01-31T20:12:38.1234Z, asdasdasdasdasdasd,...\n
2020-01-31T20:12:39.1234Z, abcdef,...\n
2020-01-31T20:12:40.1234Z, ghikjl,...\n
2020-01-31T20:12:41.1234Z, mnopqrstuv,...\n

答案不是python,而是bash

sed-n'/2020-01-31T20:12:38.1234Z/,/2020-01-31T20:12:41.1234Z/p'文件。log

输出:

2020-01-31T20:12:38.1234Z, asdasdasdasdasdasd,...\n
2020-01-31T20:12:39.1234Z, abcdef,...\n
2020-01-31T20:12:40.1234Z, ghikjl,...\n
2020-01-31T20:12:41.1234Z, mnopqrstuv,...\n
如果您想使用python

import time  
from datetime import datetime as dt  

def to_timestamp(date,forma='%Y-%m-%dT%H:%M:%S'):  
    return time.mktime(dt.strptime(date,forma).timetuple()) 

start=to_timestamp(startdate)
end=to_timestamp(enddate)
logs={}
with open("logfile.log") as f:
    for line in f:
        date=line.split(', ')[0].split('.')[0]
        logline=line.split(', ')[1].strip('\n')
        if to_timestamp(date)>=start and to_timestamp(end) <= end:
            logs[date]=logline

导入时间
从日期时间导入日期时间作为dt
def to_时间戳(日期,格式=“%Y-%m-%dT%H:%m:%S”):
return time.mktime(dt.strtime(date,forma.timetuple())
开始=到时间戳(开始日期)
结束=到时间戳(结束日期)
日志={}
将open(“logfile.log”)作为f:
对于f中的行:
日期=行。拆分(',')[0]。拆分('.')[0]
logline=line.split(',)[1]。strip('\n')
如果要在python中使用to_timestamp(date)>=开始和to_timestamp(end)

import time  
from datetime import datetime as dt  

def to_timestamp(date,forma='%Y-%m-%dT%H:%M:%S'):  
    return time.mktime(dt.strptime(date,forma).timetuple()) 

start=to_timestamp(startdate)
end=to_timestamp(enddate)
logs={}
with open("logfile.log") as f:
    for line in f:
        date=line.split(', ')[0].split('.')[0]
        logline=line.split(', ')[1].strip('\n')
        if to_timestamp(date)>=start and to_timestamp(end) <= end:
            logs[date]=logline

导入时间
从日期时间导入日期时间作为dt
def to_时间戳(日期,格式=“%Y-%m-%dT%H:%m:%S”):
return time.mktime(dt.strtime(date,forma.timetuple())
开始=到时间戳(开始日期)
结束=到时间戳(结束日期)
日志={}
将open(“logfile.log”)作为f:
对于f中的行:
日期=行。拆分(',')[0]。拆分('.')[0]
logline=line.split(',)[1]。strip('\n')

如果to_timestamp(date)>=start和to_timestamp(end),因为时间字符串在文件中已经有了很好的结构,您可以在感兴趣的时间之间进行简单的字符串比较,而无需将字符串转换为datetime对象

使用
csv
模块读取文件,使用默认的逗号分隔符,然后使用
filter()
函数在两个日期之间进行过滤

2020-01-31T20:12:38.1234Z, asdasdasdasdasdasd,...\n
2020-01-31T20:12:39.1234Z, abcdef,...\n
2020-01-31T20:12:40.1234Z, ghikjl,...\n
2020-01-31T20:12:41.1234Z, mnopqrstuv,...\n
2020-01-31T20:12:42.1234Z, wxyzdsasad,...\n
import csv

reader = csv.reader(open("logfile.log"))
filtered = filter(lambda p: p[0].split('.')[0] >= '2020-01-31T20:12:39' and p[0].split('.')[0] <= '2020-01-31T20:12:41', reader)
for l in filtered:
    print(','.join(l))
导入csv
reader=csv.reader(打开(“logfile.log”))

filtered=filter(lambda p:p[0]。split(‘.)[0]>='2020-01-31T20:12:39'和p[0]。split(‘.)[0]由于时间字符串在文件中的结构已经很好,您只需在感兴趣的时间之间进行简单的字符串比较,而无需将字符串转换为datetime对象

使用
csv
模块读取文件,使用默认的逗号分隔符,然后使用
filter()
函数在两个日期之间进行过滤

2020-01-31T20:12:38.1234Z, asdasdasdasdasdasd,...\n
2020-01-31T20:12:39.1234Z, abcdef,...\n
2020-01-31T20:12:40.1234Z, ghikjl,...\n
2020-01-31T20:12:41.1234Z, mnopqrstuv,...\n
2020-01-31T20:12:42.1234Z, wxyzdsasad,...\n
import csv

reader = csv.reader(open("logfile.log"))
filtered = filter(lambda p: p[0].split('.')[0] >= '2020-01-31T20:12:39' and p[0].split('.')[0] <= '2020-01-31T20:12:41', reader)
for l in filtered:
    print(','.join(l))
导入csv
reader=csv.reader(打开(“logfile.log”))


筛选=筛选(λp:p[0]。拆分('.')[0]>='2020-01-31T20:12:39'和p[0]。拆分('.')[0]非常感谢,但是我需要在python/javaA中这样做。dirty hack是通过
os.system
call从python文件调用bash函数。非常感谢,但是我需要在python/javaA中这样做。dirty hack是通过
os.system
call从python文件调用bash函数。上面的代码给了我一个错误类型error:strtime()参数1必须是str,而不是datetime.datetime。另外,我正在试图打印日志行,同时以字符串形式读取它们,就像文件中的“2020-01-31T20:12:38”,只需执行
print(f'{date}\t{logline}')
上面的代码给我一个错误类型error:strtime()参数1必须是str,而不是datetime.datetime。另外,我正在试图打印日志行,同时将其作为字符串读取,就像文件中的字符串一样,如“2020-01-31T20:12:38”,只需执行
print(f'{date}\t{logline})
是否有办法将输出格式化为删除方块的普通线braces@NiKK是。通过执行:
print(',')。join(l))
.Updated。通过使用rpartition,不会有输出。但是,如果仅使用p[0].partition然后我可以得到正确的输出。主要问题是最后一行没有打印。结果列表只打印到最后第二行。日志日期为'2020-01-31T20:12:41'的行没有打印printed@NiKK我已将
rpartition
更改为
split
。这将使日期字符串达到第一个点应该处理您在问题中给出的示例数据。我不知道您的实际数据是否不同?请确保使用>=并且是否有方法将输出格式化为删除正方形的普通线braces@NiKK是。通过执行:
print(',')。join(l))
.Updated。通过使用rpartition,不会有输出。但是,如果仅使用p[0].partition然后我可以得到正确的输出。主要问题是最后一行没有打印。结果列表只打印到最后第二行。日志日期为'2020-01-31T20:12:41'的行没有打印printed@NiKK我已将
rpartition
更改为
split
。这将使日期字符串达到第一个点应该使用您在问题中提供的示例数据。我不知道您的真实数据是否不同?请确保您使用>=并且此完全相同的问题在此处已经有答案:此完全相同的问题在此处已经有答案: