python将文本(txt)转excel(xls)

需要提前安装相关python包(如果通过安装anconda可以忽略)

pip3 xlwt

pip3 codecs

python代码

import xlwt
import codecs

#输入的文本文件的路径
input_txt = r'D:\Users\test0.txt'
#输出excel的路径
output_excel = r'D:\Users\finish0.xls'
#保存到excel的那个工作表
sheetName = 'test'
#从哪行哪列开始
start_row = 0
start_col = 0

wb = xlwt.Workbook(encoding = 'utf-8')
ws = wb.add_sheet(sheetName)

f = open(input_txt, encoding = 'utf-8')

row_excel = start_row
for line in f:
    line = line.strip('\n')
    line = line.split(' ')
    
    print(line)

    col_excel = start_col
    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(output_excel)

    row_excel += 1

f.close
  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值