Python将txt文件变为excel文件

首选展示一下txt文件:
在这里插入图片描述
转变为excel文件如下:
在这里插入图片描述具体代码如下:

#!/bin/env python
# -*- encoding: utf-8 -*-
#-------------------------------------------------------------------------------
# Purpose:     txt转换成Excel
# Author:      xl
# Created:     2020-1-18
# update:      ****-**-**
#-------------------------------------------------------------------------------
import xlwt

txtname = 'label.txt'
excelname = 'label.xlsx'

fopen = open(txtname, 'r')
lines = fopen.readlines()

file = xlwt.Workbook(encoding='utf-8', style_compression=0)
# 新建一个sheet
sheet = file.add_sheet('data')

i = 0
for line in lines:
    line = line.strip('\n')
    line = line.split(' ')
    picName = line[0]
    x1 = line[1]
    y1 = line[2]
    x2 = line[3]
    y2 = line[4]
    className=line[5]


    sheet.write(i, 0, picName)
    sheet.write(i, 1, x1)
    sheet.write(i, 2, y1)
    sheet.write(i, 3, x2)
    sheet.write(i, 4, y2)
    sheet.write(i, 5, className)
    i = i + 1

file.save(excelname)

参考网址:
https://blog.csdn.net/pursuit_zhangyu/article/details/83957977

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值