【发布时间】:2022-01-17 22:20:57
【问题描述】:
我正在尝试从文本文件中解析一系列消息,并使用 Python (2.7.3) 或任何其他 python 版本将它们保存为 txt 文件。
我有这样的 .txt 文件:
[#11:23][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
INFO isn't NULL
[#12:25][PERFECT][0x0015a] process returned as NULL load index[1] , length[20] , type[0]
[#13:3][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
PERFECT isn't NULL
[#4:23][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
Time is here [Tick:135055] , Time: 17, index: 608, CastedType:20002, area :0
[#15:23][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
[#16:25][PERFECT][0x0015a] process returned as NULL load index[1] , length[20] , type[0]
[#17:3][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
[#8:23][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
time is here [Tick:135055] , Time: 17, index: 608, CastedType:20002, area :0
[#16:23][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
[#14:25][PERFECT][0x0015a] process returned as NULL load index[1] , length[20] , type[0]
[#18:3][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
[#6:23][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
Time is here [Tick:135055] , Time: 17, index: 608, CastedType:20002, area :0
这是 txt 具有的所有行的类型格式,因此每一行在给定的 txt 文件中重复,并且它有自己独特的模式,如我上面所示,其中关键字 [INFO] , [PERFECT] 不会根据消息更改这些关键字值在此消息模式中不会更改。 考虑每一行都是一条新消息,因此在每一行都有一条新消息开始。
我试图在 python 中实现一个函数,该函数逐行读取 txt 文件,并且那里的所有行都有我上面提到的这种类型的模式,并以这种特定类型转储所有行:
[#12:25][PERFECT][0x0015a] process returned as NULL load index[1] , length[20] , type[0]
到另一个 txt 文件。所以如果我转到另一个 txt 文件,我会看到那里的所有行都有这种类型的消息:
[#12:25][PERFECT][0x0015a] process returned as NULL load index[1] , length[20] , type[0]
现在在从给定的 txt(input txt) 嗅探这种类型的消息之后,我需要逐行读取我生成的具有特定消息类型的新 txt 文件,然后获取加载索引值并将它们转储到另一个只有负载索引值的 txt 文件。
所以在我上面的例子中,我会得到这样的:
给定 txt 文件:(这是 .txt 文件作为输入)
[#11:23][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
INFO isn't NULL
[#12:25][PERFECT][0x0015a] process returned as NULL load index[1] , length[20] , type[0]
[#13:3][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
PERFECT isn't NULL
[#4:23][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
Time is here [Tick:135055] , Time: 17, index: 608, CastedType:20002, area :0
[#15:23][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
[#16:25][PERFECT][0x0015a] process returned as NULL load index[1] , length[20] , type[0]
[#17:3][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
[#8:23][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
time is here [Tick:135055] , Time: 17, index: 608, CastedType:20002, area :0
[#16:23][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
[#14:25][PERFECT][0x0015a] process returned as NULL load index[1] , length[20] , type[0]
[#18:3][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
[#6:23][INFO][0x0015a] it's here and it's optimally required start index[1] , length[15]
Time is here [Tick:135055] , Time: 17, index: 608, CastedType:20002, area :0
函数的结果/输出:
-
生成 txt 文件,其中包含我上面解释的 某些模式 的所有行(所有行都包含单词 [PERFECT],因此生成的 txt 文件应包含所有具有 [PERFECT] 的消息/行:
[#12:25][PERFECT][0x0015a] 进程返回为 NULL 加载 index[1] , length[20] , type[0] [#16:25][PERFECT][0x0015a] 进程返回为 NULL 加载 index[1] , length[20] , type[0] [#14:25][PERFECT][0x0015a] 进程返回为 NULL 加载 index[1] , length[20] , type[0]
-
然后为加载索引值生成另一个新的 txt 文件,在我的情况下,加载索引值在单词 load index ( load index [value] ) 的 [ ] 中找到,因此该函数应将值转储到新的 txt 文件中将加载索引 作为列 放入另一个新生成的 txt 文件中:
1 1 1
如何在 python 中解析包含上述模式和消息行的文本文件?
简单来说,我想在给定的 txt 文件上逐行(逐个消息)运行我上面解释的消息模式,然后将所有具有关键字 [PERFECT] 的消息解析到新的 txt 文件中括号,所以我将在新生成的 txt 文件中只包含关键字 [PERFECT] 的消息。 现在有了这个新生成的文件,它只嗅探了具有关键字 [PERFECT] 的消息,然后循环并传递这个新生成的文件中的每条消息(具有唯一模式 [PERFECT] 的嗅探消息)以获得值出现在每条消息中的负载索引 [值] 在我的情况下是 1 1 1 因为负载索引 [1] 在三个消息中显示为 1 。加载索引值应转储到另一个新的 txt 文件中,该文件以加载索引值作为列。
非常感谢您的合作!
【问题讨论】:
标签: python python-3.x python-2.7 testing software-design