简体   繁体   English

如何根据条件在python中解析文本文件

[英]How to Parse text file in python based on condition

I have a text file that I want to parse based on the condition that if I find the match phrase in the line then I have to jump to the next line to fetch the value{unfortunately that's how the reports logs are generated}. 我有一个文本文件,要根据以下条件进行分析:如果我在该行中找到匹配短语,则必须跳到下一行以获取值{很遗憾,这就是生成报告日志的方式}。 I have created _dict to check my key and fetch my values in the next line. 我创建了_dict以检查我的密钥并在下一行中获取我的值。

Lines = f1.readlines()
numlines = len(Lines)
f1.close()
f1 = open('Testlog.txt','r')
f2 =open('writetoFile','r+') 
f3 =open('Results.txt','w')
new_line="Test Name       SubTest         passed      failed         status  "
f3.write(new_line)
f3.write("\n")
while i < numlines:

        line=f1.readline()
        if "Test Name" in line:

            f2.write(line)
            i=i+1
            line =f1.readline()

            if "true" in line:
                f2.write(line)
                line = line.strip('\n ')
                #print line
                data = re.split(r"\s{2,}",line)
                Test_Name=data[4]
                SubTest=data[6]
                passed=data[7]
                failed=data[8]
                status=data[9]
                result = Test_Name + "            " + SubTest + "      " +  passed + "         " + failed + "      "   +  status
                print result 

                f3.write(result)
                f3.write("\n")
                i=i+1

I was wondering if there better way to do this 我想知道是否有更好的方法可以做到这一点

What is your method for parsing the line? 您解析行的方法是什么? Can you post sample code, that will help. 您可以张贴示例代码吗,这会有所帮助。

To answer your second question, you could make a Dictionary in which each key refers to a List, then you can use a for loop to iterate through each of the values (or whatever you'll need) 要回答第二个问题,您可以制作一个Dictionary,其中每个键都指向一个列表,然后可以使用for循环遍历每个值(或您需要的任何值)

    foo = { 1 : ['a','b','c'] }

    for value in foo[1]:
        print(value)

prints abc 打印abc

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

相关问题 如何在python中解析文本文件 - How to parse text file in python 如何根据 python 中的条件提取文本 - How to extract text based on a condition in python 如何根据条件替换Excel文件中的文本? - How to Replace Text in Excel file based on condition? 如何解析可变内容,然后基于该内容评估条件,python - how to parse a variable content and then evaluate a condition based on that , python 如何根据特殊条件在文本文件的每一行中删除变量空格 - 在Python中使用单行程? - How to strip variable spaces in each line of a text file based on special condition - one-liner in Python? 如何解析和减少多个if条件到文本文件中的一行? - how to parse and reduce multiple if condition to one line in text file? Python:如何根据两个不同列中的大于条件删除文本文件中的数据行? - Python: How to delete rows of data in a text file based on a greater than condition in two different columns? 如何使用 Python 使用 Pipe 分隔符拆分文本文件,然后根据条件选择列? - How to split text file with Pipe delimiter using Python and then pick columns based on condition? 根据列号和条件删除文本文件中的记录-python - Delete records in a text file based on column number and condition - python Python-根据条件读取文本文件中的特定行 - Python - Read specific lines in a text file based on a condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM