为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

繁星点点滴滴 2021-08-11 17:11:13
从以下输入文件中,我想拆分testname和关联logdetails输入文件:2/1/1/2/tasdf.c:LOG:        backslash-newline should be deleted before tokenizing    No diagnostics lineRESULT: 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 failedRESULT: 2/1/1/2/tlasdf.c                                          FAILED----------------------------------------2/2/4/1/texasdfgen(0):LOG:    511 external identifiers in one source file    Compilation failed ungracefullyRESULT: 2/2/4/1/textasdf.gen                                    FAILED用于拆分的代码:import reimport sys#inputfileTEST = sys.argv[1]#Open input file and match testnamedef 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 detailsdef 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.
查看完整描述

2 回答

?
HUWWW

TA贡献1874条经验 获得超12个赞

首先做以下修改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)])) 


查看完整回答
反对 回复 2021-08-11
  • 2 回答
  • 0 关注
  • 157 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信