資源簡介
使用python語言,將txt文本文檔批量轉(zhuǎn)為excel文件,用于處理大量txt文檔
代碼片段和文件信息
#?-*-?coding:?utf-8?-*-
import?datetime
import?time
import?os
import?sys
import?xlwt?#需要的模塊
path?=?r“G:\temp_prec\2016\txt“
def?txt2xls(fp?file):
????if?os.path.exists(fp):
????????print?fp
????????print?“正在處理“?fp
????????f?=?open(fp)
????????wb?=?xlwt.Workbook()
????????ws1?=?wb.add_sheet(“Sheet1“)
????????i?=?0
????????for?line?in?f.readlines():
????????????j?=?0
????????????for?item?in?line.split(‘\t‘):
????????????????try:
????????????????????item?=?item.strip().decode(‘utf-8‘)
????????????????except?UnicodeDecodeError:
????????????????
評(píng)論
共有 條評(píng)論