資源簡介
利用python中的xlrd、xlwt、re以及xlutils.copy模塊,對txt文檔的數據逐行進行分析并按一定規則轉錄至xls文檔中,按具體需求可對文檔中的正則表達式以及flag進行更改
代碼片段和文件信息
#!/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個表
sh1?=?wb.get_sheet(1)#第2個表
sh2?=?wb.get_sheet(2)#第3個表
sh3?=?wb.get_sheet(3)#第4個表
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#根據flag確定填寫數據的sheet,此處共有四個s
評論
共有 條評論