python读取txt数据写入excel

在公司接到一个任务,从txt中抓取数据写入excel,txt格式固定,并且有多个txt文件

先安装excel的读写支持,参考:https://www.cnblogs.com/cllovewxq/p/5363636.html

就是下载xlrd和xlwt,进入该目录分别运行python setup.py install,这个程序只用到写入操作--xlwt

思路:

    逐个打开txt文件,抓取指定位置,使用list列表存取数据,写入excel中.

代码:

# coding=utf-8
import re
from os.path import os
from xlwt.Workbook import Workbook
mypath = 'D:/alltxt/'
myfiles = os.listdir(mypath)
fileList = []
excellist = []
for f in myfiles:
        if(os.path.isfile(mypath + '/' + f)):
            if os.path.splitext(f)[1] == '.txt':
                fileList.append(f)
            # 添加文件          
for ff in fileList:
    f = open(mypath+ff,'r',encoding='utf-8')
#f = open('D:/alltxt/test.txt', 'r',encoding='ut

  • 3
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值