如何在python中将.txt转换为.xls?

时间:2016-02-10 08:54:06

标签: python excel python-2.7 parsing

with open(output_filename, 'w') as f_output:
    for dirpath, dirnames, filenames in os.walk(directory): 
        for filename in filenames:
             if filename.startswith('VCALogParser_output'): 
                cur_file = os.path.join(dirpath, filename)
                with open(cur_file, "r") as f_input:
                    f_input.seek(0, os.SEEK_END)
                    backup = min(back_up , f_input.tell())
                    f_input.seek(-backup, os.SEEK_END)
                    last_lines = ''.join(f_input.readlines()[-6:])
                    try:
                        summary = ', '.join(re.search(r'(\d+ warning\(s\)).*?(\d+ error\(s\)).*?(\d+ scenarios\(s\) FAILED)', last_lines, re.S).groups())
                    except AttributeError:
                        summary = "VCA DID NOT START"
                    f_output.write('{}: {}\n'.format(filename, summary))

上面的代码是解析文件并从文件中读取一些数据并将其作为输出以.txt的形式输出。但我想把它作为一个.xls输出。谁能告诉我如何将下面的.txt转换为.xls?

输出.txt格式:

VCALogParser_output_ARW.log: 12 warning(s), 85 error(s), 32 scenarios(s) FAILED
VCALogParser_output_CZC.log: 18 warning(s), 33 error(s), 14 scenarios(s) FAILED
VCALogParser_output_DUN.log: VCA DID NOT START
VCALogParser_output_FRC.log: 12 warning(s), 34 error(s), 12 scenarios(s) FAILED
VCALogParser_output_GED.log: 39 warning(s), 79 error(s), 27 scenarios(s) FAILED
VCALogParser_output_MNT.log: 41 warning(s), 76 error(s), 33 scenarios(s) FAILED
VCALogParser_output_NON.log: 25 warning(s), 50 error(s), 14 scenarios(s) FAILED
VCALogParser_output_PTB.log: VCA DID NOT START
VCALogParser_output_PTP.log: 31 warning(s), 35 error(s), 21 scenarios(s) FAILED
VCALogParser_output_SPM.log: 58 warning(s), 150 error(s), 57 scenarios(s) FAILED
VCALogParser_output_SWS.log: 14 warning(s), 23 error(s), 11 scenarios(s) FAILED

1 个答案:

答案 0 :(得分:1)

尝试xlutils模块

"用于处理需要xlrd和xlwt"

的Excel文件的实用程序