【问题标题】:Jump into the next line in readlines inside for loop跳转到 for 循环内 readlines 的下一行
【发布时间】:2019-06-19 07:04:21
【问题描述】:

我正在编写一个代码来从一个很大的Source.txt 文件中提取一些有用的东西。 我的源测试文件示例如下:

Test case AAA
Current Parameters:
    Some unique param : 1
    Some unique param : 2
    Some unique param :     3
    Some unique param : 4
*A line of rubbish*
*Another line of rubbish*
*Yet another line of rubbish*
*More and more rubbish*
Test AAA PASS
Test case BBB
Current Parameters:
    Some unique param : A
    Some unique param : B
    Some unique param :     C
    Some unique param : D
*A line of rubbish*
*Another line of rubbish*
*Yet another line of rubbish*
*More and more rubbish*
Test BBB PASS

现在我正在编写代码以仅提取 Test caseCurrent Parameters

processed = []

def main():
    source_file = open("Source.txt","r") #Open the raw trace file in read mode
    if source_file.mode == "r":
        contents = source_file.readlines()   #Read the contents of the file
        processed_contents = _process_content(contents)
        output_file = open("Output.txt","w")
        output_file.writelines(processed_contents)
        pass

def _process_content(contents):
    for raw_lines in contents:
        if "Test case" in raw_lines:
            processed.append(raw_lines)
        elif "Current Parameters" in raw_lines:
            processed.append(raw_lines)
            #I am stuck here
        elif "PASS" in raw_lines or "FAIL" in raw_lines:
            processed.append(raw_lines)
            processed.append("\n")
    return processed

#def _process_parameters():


if __name__ == '__main__':
    main()

Current Parameters 行之后,我想获取每一个Some unique param,这些Some unique param 并不总是相同,并附加到processed 列表中,这样它也会在我的Output.txt 中注明

我想要的输出是:

Test case AAA
Current Parameters:
    Some unique param : 1
    Some unique param : 2
    Some unique param :     3
    Some unique param : 4
    Test AAA PASS
Test case BBB
Current Parameters:
    Some unique param : A
    Some unique param : B
    Some unique param :     C
    Some unique param : D
    Test BBB PASS

如果你看到了,我想删除所有垃圾行。请注意,我的Source.txt 中有很多垃圾。我不知道如何从那里转到下一个raw_lines。感谢您的帮助。

【问题讨论】:

  • @Adam.Er8,很好。但我不知道如何继续到 Param 1 行
  • 为什么elif "Param" in raw_lines 不起作用?它是否也捕获了一些垃圾?
  • 好的,我忘了说,raw_lines 中的 Param 并不总是包含单词 Param。现在将编辑问题
  • 好的,这些是Current Parameters: 之后唯一的缩进行吗?因为我们可以做的是每次遇到Current Parameters:时标记一个flag,然后将所有缩进的行都添加进去,当我们找到没有缩进的行时,将flag取下并返回正常模式
  • 刚刚编辑了问题,我的参数将是唯一的,并且名称不同。很抱歉造成混乱

标签: python readlines


【解决方案1】:

这是使用正则表达式的一种方法。

例如:

import re

result = []
with open(filename) as infile:
    for raw_lines in infile:
        if "Test case" in raw_lines:
            result.append(raw_lines)
        if "Current Parameters" in raw_lines:
            result.append(raw_lines)
            raw_lines = next(infile)                        #next() to move to next line. 
            while True:
                m = re.search(r"(?P<params>\s*\w+\s*:\s*\w+\s*)", raw_lines)    
                if not m:
                    break
                result.append(m.group("params"))
                raw_lines = next(infile)
        if "PASS" in raw_lines or "FAIL" in raw_lines:
            result.append(raw_lines)
            result.append("\n")
print(result)

输出:

['Test case AAA\n',
 'Current Parameters:\n',
 ' param : 1\n',
 ' param : 2\n',
 ' param :     3\n',
 ' param : 4\n',
 'Test AAA PASS\n',
 '\n',
 'Test case BBB\n',
 'Current Parameters:\n',
 ' param : A\n',
 ' param : B\n',
 ' param :     C\n',
 ' param : D\n',
 'Test BBB PASS',
 '\n']

【讨论】:

  • 这似乎是正确的解决方案。我会尝试更新你
  • 这按预期工作,稍作修改,非常感谢
【解决方案2】:

很难确定这是否可行,因为我对垃圾行的格式一无所知,但我认为您可以像您一样检查该行是否包含"Param"正在为其他线路做:

def _process_content(contents):
    for raw_line in contents:
        if "Test case" in raw_line:
            processed.append(raw_line)
        elif "Current Parameters" in raw_line:
            processed.append(raw_line)
        elif "Param" in raw_line:
            processed.append(raw_line)
        elif "PASS" in raw_line or "FAIL" in raw_lines:
            processed.append(raw_line)
            processed.append("\n")
    return processed

【讨论】:

  • 如果参数行不总是包含“Param”,而是总是以一些空格开头,你可以这样做elif raw_line.startswith(" ")
  • 我刚刚编辑了我的问题,很抱歉造成混乱。
【解决方案3】:

您可以使用正则表达式反向引用(例如\2)来拆分测试用例(regex101):

import re

data = '''Test case AAA
Current Parameters:
    Some unique param : 1
    Some unique param : 2
    Some unique param :     3
    Some unique param : 4
*A line of rubbish*
*Another line of rubbish*
*Yet another line of rubbish*
*More and more rubbish*
Test AAA PASS
Test case BBB
Current Parameters:
    Some unique param : A
    Some unique param : B
    Some unique param :     C
    Some unique param : D
*A line of rubbish*
*Another line of rubbish*
*Yet another line of rubbish*
*More and more rubbish*
Test BBB PASS'''

for g in re.findall(r'(^Test case ([A-Za-z]+)\s+Current Parameters:(?:[^:]+:.*?$)*)+.*?(Test \2 (PASS|FAIL))', data, flags=re.DOTALL|re.M):
    print(g[0])
    print(g[2])

打印:

Test case AAA
Current Parameters:
    Some unique param : 1
    Some unique param : 2
    Some unique param :     3
    Some unique param : 4
Test AAA PASS
Test case BBB
Current Parameters:
    Some unique param : A
    Some unique param : B
    Some unique param :     C
    Some unique param : D
Test BBB PASS

【讨论】:

    【解决方案4】:

    您可以使用str.startswith() 过滤掉您想要的行,然后将这些行重新写入文件。我还将在":" 上拆分行,并检查长度为 2 的 idd 以查找参数。将行转换为全小写也是安全的,因此您可以进行无大小写匹配,因此它认为"Test""test" 不同。

    演示:

    lines = []
    with open("source.txt") as f:
        for line in f:
            lowercase = line.lower()
            if (
                lowercase.startswith("test")
                or lowercase.startswith("current parameters:")
                or len(lowercase.split(":")) == 2
            ):
                lines.append(line)
    
    with open("source.txt", mode="w") as o:
        for line in lines:
            o.write(line)
    

    source.txt:

    Test case AAA
    Current Parameters:
        Some unique param : 1
        Some unique param : 2
        Some unique param :     3
        Some unique param : 4
    Test AAA PASS
    Test case BBB
    Current Parameters:
        Some unique param : A
        Some unique param : B
        Some unique param :     C
        Some unique param : D
    Test BBB PASS
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-09
      • 1970-01-01
      • 2015-11-11
      • 1970-01-01
      • 1970-01-01
      • 2021-07-11
      • 2016-12-12
      • 2015-07-02
      相关资源
      最近更新 更多