簡體   English   中英

Python讀取tsv文件並評估

[英]Python read tsv file and evaluate

我有准備如下的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:   

我想做的是:-開始從測試,案例中讀取文件-分別讀取每個測試案例:testcase1,testcase2..n(每個測試案例以不帶制表符開頭,測試用例主體以制表符開頭)-評估所有測試用例是否具有表達式“ startAdvErrorTrace”我在tsv中有大約50個測試用例的“ startAdvDumpEvents”,想評估所有文件

我在開發中完全是綠色的。 我發現一些想法,如將tsv讀取csv文件。 但是我不知道如何達到我的期望

我不知道這是什么文件格式,但是您可以執行以下操作:

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)

這是一個用於根據每個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

希望能有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM