读取文本文件并写入excelsheet

时间:2018-11-08 04:14:54

标签: python

我正在尝试读取文本文件并提取testnamelog details并将其写入Excel工作表。 这是输入文本文件格式:

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

预期输出:

excel sheet output

我能够获得testname的详细信息,但是我的代码未获取日志详细信息。 这是python代码:

import xlwt
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('--'):
                    break
             # else process lines from section
                array.append(line)
print(array)    
book = xlwt.Workbook(encoding="utf-8")
sheet1 = book.add_sheet("Reports")

#sample code to write into excel sheet 
testnames = testname(TEST)
for index, testname in enumerate(testnames, start=1):
    sheet1.write(index, 1, testname)
logdetails = logdetail(TEST)
for index, log in enumerate(logdetails, start=1):
    sheet1.write(index, 2, log)

book.save("test.xls")

0 个答案:

没有答案