資源簡(jiǎn)介
利用python中的xlrd、xlwt、re以及xlutils.copy模塊,對(duì)txt文檔的數(shù)據(jù)逐行進(jìn)行分析并按一定規(guī)則轉(zhuǎn)錄至xls文檔中,按具體需求可對(duì)文檔中的正則表達(dá)式以及flag進(jìn)行更改
代碼片段和文件信息
#!/usr/bin/env?python3
#coding:?utf-8
import?xlrd
import?xlwt
import?re
from?xlutils.copy?import?copy
path_txt?=?“C:/Users/admin/Desktop/value.txt“#txt文件路徑
txt?=?open(path_txt‘r‘)
path_excel?=?“C:/Users/admin/Desktop/test.xls“#excel文件路徑
rb?=?xlrd.open_workbook(path_excel)#打開excel文件
wb=copy(rb)#write
sh0?=?wb.get_sheet(0)#第1個(gè)表
sh1?=?wb.get_sheet(1)#第2個(gè)表
sh2?=?wb.get_sheet(2)#第3個(gè)表
sh3?=?wb.get_sheet(3)#第4個(gè)表
flag=0#sheet
flag1=0
row=2#position
for?line?in?open(path_txt):
????line?=?txt.readline()#逐行
????line?=?line.strip()
????if?flag?==?0:
????????a?=?sh0
????elif?flag?==?1:
????????a?=?sh1
????elif?flag?==?2:
????????a?=?sh2
????elif?flag?==?3:
????????a?=?sh3#根據(jù)flag確定填寫數(shù)據(jù)的sheet,此處共有四個(gè)s
評(píng)論
共有 條評(píng)論