简体   繁体   English

Python读取tsv文件并评估

[英]Python read tsv file and evaluate

I have tsv file which is prepared like: 我有准备如下的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:   

What i want to do is: - start reading file from Test, Cases - read separatly every testcase: testcase1, testcase2..n (every testcase starts without tab, testcase body starts with tab) - evaluate if all testcases has expresions "startAdvErrorTrace" "startAdvDumpEvents" etc I have in tsv about 50 testcases and want evaluate all file 我想做的是:-开始从测试,案例中读取文件-分别读取每个测试案例:testcase1,testcase2..n(每个测试案例以不带制表符开头,测试用例主体以制表符开头)-评估所有测试用例是否具有表达式“ startAdvErrorTrace”我在tsv中有大约50个测试用例的“ startAdvDumpEvents”,想评估所有文件

I'm totally green in developing. 我在开发中完全是绿色的。 I have found some ideas like read csv file as tsv. 我发现一些想法,如将tsv读取csv文件。 But i don't know how to achieve my expectations 但是我不知道如何达到我的期望

I don't know what file format this is, but you can do something like this: 我不知道这是什么文件格式,但是您可以执行以下操作:

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)

Here's a little script to parse the flags per Testcase. 这是一个用于根据每个Testcase解析标志的小脚本。 The output is: 输出为:

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

And the script is: 脚本是:

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

Hope that helps. 希望能有所帮助。

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

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