在Python中组合日志文件和按时间排序



首先,我对Python和编程非常陌生,所以如果这是一个愚蠢的明显问题,请原谅我。

我有一个未定义的数量(可能是10+)的日志文件与目录中的其他随机文件混合在一起,我需要将这些文件合并到一个单独的文件中,并按每行开头的时间戳排序。日志文件是。txt,在同一目录下还有其他非。txt文件,所以我要让这个脚本的用户输入每个日志文件作为参数。

现在,在你把这个标记为重复之前,我在这里看了4页的搜索结果,没有的问题有一个我可以使用的答案。

到目前为止,我有以下几种工作Python代码:
log_file_name = 'logfile.txt'
import sys
import fileinput
from Tkinter import Tk
from tkFileDialog import askopenfilenames
logfile = open(log_file_name, 'w+')
logfile.truncate()
logfile.seek(0)
# get list of file names
print "Opening File Dialog"
Tk().withdraw()
files = askopenfilenames(title='Select all logs you would like to compile.')
for index in range(len(files)):
    print "Loop ", index
    print "--- Debug message: Reading a file... ---"
    logdata = open((files[index])).readlines()
    print "--- Debug message: Finished reading. Writing a file... ---"
    # turns logdata into a string and writes it to logfile
    logfile.write(''.join(logdata))
    logfile.write("n")
print ""
print "Exited for loop."
logfile.close()

上面的代码将您选择的所有文件的内容放入一个文本文件中,但是它没有对它们进行排序。

我正在考虑使用正则表达式搜索括号内的数字,然后根据该排序每行…?

以下是一些示例日志文件内容。

[xx.xxxxxx] [Text] Text : Text: xxx
[xx.xxxxxx] [Text] Text : Text: xxx
[xx.xxxxxx] [Text] Some text.
There could be multiple lines of text here
These lines could include [brackets.] :(
[xx.xxxxxx] [Text] Text : Text: xxx

[xx。

为系统启动后的时间戳,单位为秒。

由于时间戳位于每条记录的开头,因此您可以进行排序。如果花费的时间太长,那么您可能需要对输入的每个日志文件进行排序并合并到最终列表

import pprint
file_1="""[92.5] Text Text : Text: xxx
[91.5] Text Text : Text: xxx"""
file_2="""[91.7] [Text] Some text.
Some text of variable size, may be on multiple lines. Number of lines is variable also. 
[90.5] [Text] Some text.
Some text of variable size, may be on multiple lines. Number of lines is variable also."""
## Write data to some test log files
with open("./log_1.txt", "w") as fp_out:
    fp_out.write(file_1)
with open("./log_2.txt", "w") as fp_out:
    fp_out.write(file_2)
def input_recs(f_name):
    recs=open(f_name, "r").readlines()
    ## assume you want to omit text only lines
    return_list=[rec.strip() for rec in recs if rec[1].isdigit()]
    return return_list
sorted_list=[]
for f_name in ["log_1.txt", "log_2.txt"]:
    recs=input_recs(f_name)
    sorted_list.extend(recs)
sorted_list.sort()
pprint.pprint(sorted_list)

当你没有得到好的答案时,这意味着你没有提出好的问题。"识别每条消息,然后对消息进行排序,而不是每一行"是什么意思?为了说明通常如何做到这一点,我将假设您希望将没有时间戳的行包含在前一个时间戳中。您必须以某种顺序获得数据,这些顺序可以根据特定的rec(ord)进行排序。有两种方法可以做到这一点:使用字典或列表的列表。下面使用列表的列表,并简单地将非时间戳的记录(ord)s附加到前一个时间戳的记录中,因此所有记录都以时间戳开始,并且可以对列表进行排序。到目前为止,你应该了解所涉及的一般原理。

file_1="""[92.5] [Text1[ Text : Text: xxx
[91.5] [Text2[ Text : Text: xxx
[92.5] [Text2.5] Some text.
[90.5] [Text3] Some text"""
file_2="""[91.7] [Text4] Some text.
Some text of variable size, may be on multiple lines. Number of lines is variable also. 
[90.5] [Text5] Some text.
Some text of variable size, may be on multiple lines. Number of lines is variable also."""
## Write data to some test log files
with open("./log_1.txt", "w") as fp_out:
    fp_out.write(file_1)
with open("./log_2.txt", "w") as fp_out:
    fp_out.write(file_2)
def input_recs(f_name):
    return_list=[]
    append_rec=""
    with open(f_name, "r")as fp_in:
        for rec in fp_in:
            if rec[1].isdigit():
                ## next time stamp so add append_rec to return_list and
                ## create a new append_rec that contains this record
                if len(append_rec): 
                    return_list.append(append_rec)
                append_rec=rec
            else:
                append_rec += rec  ## not a time stamp
    ## add last rec
    if len(append_rec): 
        return_list.append(append_rec)
    return return_list
sorted_list=[]
for f_name in ["log_1.txt", "log_2.txt"]:
    recs_list=input_recs(f_name)
    sorted_list.extend(recs_list)
sorted_list.sort()
import pprint
pprint.pprint(sorted_list)  ## newlines are retained

最新更新