Python读取tsv文件并进行评估

时间:2014-02-06 11:33:37

标签: python file text-parsing

我有tsv文件,其准备如下:

*Settings*                          
Force, Tags FakeTag                     
Resource    ../../robot_resources/global.tsv                        

*Test, Cases*                           
Testcase1   [Documentation] PURPOSE:                    
    ... Test, checking,,                
    ...                     
    ...                     
    ... PRECONDITION:                   
    ... set,                    
    ... make, sure, that,                   
    ...                     
    ...                                     
    [Setup] stopProcessAndWaitForIt                 
    startAdvErrorTrace                      
    startAdvDumpEvents                      
    stopProcessAndWaitForIt                             
    stopAndDownloadAdvDumpEvents                                

Testcase2   [Documentation] PURPOSE:                    
    ... Test, checking,, if,                
    ...                     
    ...                     
    ... PRECONDITION:   

我想做的是: - 从 Test,Cases 开始读取文件 - 分别读取每个测试用例:testcase1,testcase2..n(每个测试用例在没有制表符的情况下启动,测试用例正文以制表符开头) - 评估所有测试用例是否具有表达式“startAdvErrorTrace”“startAdvDumpEvents”等 我在tsv中有大约50个测试用例,并且想要评估所有文件

我在发展中完全是绿色的。我发现了一些想法,如读取csv文件作为tsv。但我不知道如何实现我的期望

2 个答案:

答案 0 :(得分:0)

我不知道这是什么文件格式,但你可以这样做:

items = ("startAdvErrorTrace", "startAdvDumpEvents") # add more as desired
import re
with open("testfile") as infile:
    # skip contents until Test Cases
    contents = re.search(r"(?s)\*Test, Cases\*.*", infile.read())
    cases = contents.split("\n\n") # Split on two consecutive newlines
    for case in cases:
        if not all(item in case for item in items)
            print("Not all items found in case:")
            print(case)

答案 1 :(得分:0)

这是一个解析每个Testcase标志的小脚本。输出是:

Flags per testcase:
{1: ['startAdvErrorTrace', 'startAdvDumpEvents'], 2: []}

脚本是:

usage = {}
flags = ('startAdvErrorTrace', 'startAdvDumpEvents')
with(open(testfile)) as f:
    lines = [line.strip() for line in f.readlines()]

    start_parsing = False
    for line in lines:
        if 'Test, Cases' in line:
            start_parsing = True
            continue

        if parse_cases:
            if 'Testcase' in line:
                case_nr = int(re.findall('\d+', line)[0])
                usage[case_nr] = []
                continue

            for flag in flags:
                if flag in line:
                    usage[case_nr].append(flag)

print 'Flags per testcase:'
usage

希望有所帮助。