简体   繁体   English

解析文本文件,仅捕获具有特定字符的两行之间的行

[英]Parse text file and only capture lines between two lines with specific characters

I have to write a python script that parses a log text file, but the only data of interest is that of the "Test" being examined. 我必须编写一个用于解析日志文本文件的python脚本,但是唯一感兴趣的数据是正在检查的“测试”数据。 The text file is in the following general format: 文本文件具有以下常规格式:

Test 1
[lines of data]

Test 2
[lines of data]

...

The [lines of data] represents what could be either many or few lines of data from the said test, and the log file can have any number of tests. [数据行]表示来自所述测试的多行或几行数据,并且日志文件可以具有任意数量的测试。 So if I only wanted to look at "Test 1", what I want my script to do is extract all the information between "Test 1" and "Test 2" but have it stop reading before "Test 2". 因此,如果我只想看“测试1”,我希望我的脚本要做的是提取“测试1”和“测试2”之间的所有信息,但在“测试2”之前停止读取。

The catch is that I want my script to do the same thing even if I'm looking to parse the data from, say Test 12, and have it stop before Test 13, because there can be any number of tests in said file. 问题在于,即使我要解析测试12中的数据,并希望在测试13之前停止它,我也希望脚本执行相同的操作,因为在该文件中可以有任意数量的测试。 How would I go about this? 我将如何处理?

May i suggest using the following code: 我可以建议使用以下代码:

import re

with open("1new.txt","r") as file:
    eaw=file.read()

num_of_tests=2
for i in range(1,num_of_tests):
    extract=re.search(r"(?<=Test %s)(.*)(?=Test %s)"%(i,i+1),eaw,re.DOTALL).group()
    print(extract)

The OUTput will be: 输出将是:

[lines of data]
[lines of data]

Can add additional lines to append the extracted lines into a different file: 可以添加其他行以将提取的行附加到其他文件中:

with open("extracted.txt","a") as file2:

    file2.write(extract)

The regex will simply look for matches in between Test 1 and Test 2 and so on. 正则表达式只会在测试1和测试2之间寻找匹配项,依此类推。 it uses positive lookbehind "?<=" and positive lookahead "?=" to look for matches and with ".*" you will be able to get everything in between the matches. 它使用“?<=”后面的正向lookahead和“?=“”后面的正向lookahead查找匹配项,使用“。*”,您将能够获得匹配项之间的所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM