如何使用 python groupby 从给定的文本文件中拆分测试名称和日志详细信息

从以下输入文件中,我想拆分testname和关联logdetails


输入文件:


2/1/1/2/tasdf.c:


LOG:

        backslash-newline should be deleted before tokenizing

    No diagnostics line

RESULT: 2/1/1/2/tasdf.c                                          FAILED


----------------------------------------

2/1/1/2/tlasdf.c:


LOG:

+++ stderr ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

tlasdf.c:15:5: error: use of undeclared identifier '_t'

    t x[] = L\

    ^

ls: cannot access '*.o': No such file or directory

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    | T | Translation Phases | 2 | \\ | L | 2 |

    Compilation failed

RESULT: 2/1/1/2/tlasdf.c                                          FAILED


----------------------------------------

2/2/4/1/texasdfgen(0):


LOG:

    511 external identifiers in one source file

    Compilation failed ungracefully

RESULT: 2/2/4/1/textasdf.gen                                    FAILED

用于拆分的代码:


import re

import sys


#inputfile

TEST = sys.argv[1]


#Open input file and match testname

def testname(FILE):

    testlist=[]

    for line in open(FILE, 'r+'):

        match1 = re.search(r'.*\.c\:$|.*\.gen\(\d+\)\:$', line)

        if match1:

            testname = match1.group(0)

            testlist.append(testname)

    return(testlist)


#Open input file and match log details

def logdetail(FILE):

array = []

with open(TEST) as f:

    for line in f:

        if line.startswith('LOG:'):

            for line in f:

                if line.startswith('RESULT:'):

                    break

             # else process lines from section

                array.append(line)

print(array)    

testname = testname(TEST)

for test in testname:

    print (test)        


loddetails = logdetail1(TEST)

for log in loddetails:

    print(log)

testname正确打印并且日志详细信息存在于数组中,但如何testname与logdetails.


繁星点点滴滴
浏览 158回答 2
2回答

HUWWW

首先做以下修改logdetail():def logdetail(FILE):    collect = False    array = []    current = []    with open(FILE, 'r+') as f:        for line in f:            if line.startswith('LOG:'):                collect = True            else:                if line.startswith('RESULT: '):                    collect = False                    array.append(current)                    current=[]                if collect:                    current.append(line.strip())    return(array)然后用它来打印(假设总是len(testname) = len(logdetails))testname = testname(TEST)loddetails = logdetail1(TEST)for test in testname:    print (test + '\t' +  " ".join(logdetail1[testname.index(test)])) 
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python