Python txt convert excel

Background: Lean or Agile project , the test program or test case is said to be CTO or get out of the xmind or other mind mapping inside. Sometimes the case is more text, you need to deal with what turned execl (currently still rely most cases execl to organize) to import the corresponding platform inside. Lean case for the following three-step explanation: if. . . when. . . then. . .

If it is the precondition, when the equivalent of performing operation or step, then a desired value or the expected result.

There are cases title.

Tools: Notepad ++ Tip: column operations: Alt + rollers following examples:

TestCase.txt file, neat, will be a "+" separated by

TxtTransFormExecl.py file

python code:

import xlwt #.py文件要在和xlwt和xlrd同一层目录,不然报错

wb = xlwt.Workbook(encoding='utf-8')
ws = wb.add_sheet( 'Sheet1') #sheet页第一页

f = open('TestCase.txt', encoding='gbk') #.py文件和TestCase.txt同一目录,第一个参数是路径

row_excel = 0 #行

for line in f:
    line = line.strip('\n') #去掉换行符
    line = line.split('+') #每一行以"+"分隔

    print(line) #测试

    col_excel = 0 #列
    len_line = len(line)
    for j in range(len_line):
        print(line[j]) #测试
        ws.write(row_excel, col_excel, line[j])
        col_excel += 1
        wb.save('TestCase.xls') #输出在同一目录

    row_excel += 1

f.close

Operating results, generate TestCase.xls file, as follows:

OK. This gadget when finishing thousands of cases very so, please flexible use!

 

Published 221 original articles · won praise 207 · views 170 000 +

Guess you like

Origin blog.csdn.net/qq_29720657/article/details/104022694