Python实现读取txt文件并转换为excel的方法示例
作者:江枫渔火2017 发布时间:2023-07-25 15:20:16
标签:Python,txt,excel
本文实例讲述了Python实现读取txt文件并转换为excel的方法。分享给大家供大家参考,具体如下:
这里的txt文件内容格式为:
892天平天国定都在?A开封B南京C北京(B)
Python代码如下:
# coding=utf-8
'''''
main function:主要实现把txt中的每行数据写入到excel中
'''
#################
#第一次执行的代码
import xlwt #写入文件
import xlrd #打开excel文件
import os
txtFileName = 'questions.txt'
excelFileName = 'questions.xls'
if os.path.exists(excelFileName):
os.remove(excelFileName)
fopen = open(txtFileName, 'r')
lines = fopen.readlines()
#新建一个excel文件
file = xlwt.Workbook(encoding='utf-8',style_compression=0)
#新建一个sheet
sheet = file.add_sheet('data')
############################
#写入写入a.txt,a.txt文件有20000行文件
i=0
j=0
for line in lines:
indexA = line.find('A')
questionStr = line[0:indexA]
questionStr.lstrip()
indexB = line.find('B')
answerA = line[indexA:indexB]
indexC = line.find('C')
indexE = line.find('(')
answerB = ''
if indexC>0:
answerB = line[indexB:indexC]
else:
answerB = line[indexB:indexE]
indexD = line.find('D')
answerC = ''
answerD = ''
if indexD>0:
answerC = line[indexC:indexD]
answerD = line[indexD:indexE]
else:
answerC = line[indexC:indexE]
answer = line[line.find('('):line.find(')')]
cindex = 0
questionStrCopy = ''
for c in questionStr:
if cindex<3:
if c>='0' and c<='9':
questionStrCopy = questionStr[cindex+1:]
cindex = cindex + 1
answerA = answerA[1:]
answerB = answerB[1:]
answerC = answerC[1:]
answerD = answerD[1:]
answer = answer.strip('(')
print answer
print questionStrCopy, answerA, answerB, answerC, answerD, answer
questionStrCopy = questionStrCopy.lstrip()
if questionStrCopy=='' or answerA=='' or answer=='':
continue
sheet.write(i, 0 , questionStrCopy)
sheet.write(i, 1 , answerA)
sheet.write(i, 2 , answerB)
sheet.write(i, 3 , answerC)
sheet.write(i, 4 , answerD)
sheet.write(i, 5 , answer)
i = i + 1
file.save(excelFileName)
希望本文所述对大家Python程序设计有所帮助。
来源:https://blog.csdn.net/Jesse__Zhong/article/details/79690617


猜你喜欢
- 在 PHP 中实现异步定时多任务消息推送的方式有多种,其中一种常用的方式是使用异步任务队列。以下是一个简单的步骤:安装和配置消息队列服务(如
- Python 内置的 zipfile 模块可以对文件(夹)进行ZIP格式的压缩和读取操作。要进行相关操作,首先需要实例化一个 ZipFile
- 有一个txt文本如下:151 151 1234561 156421 214156 1523132 031320现希望将两行合并为一行,并将
- 1.我们可以为每一个实例对象增加方法。也就是说我们在每次使用‘类'之外的方法时候,都需要创建一次。 function D
- 一般一个网站的首页访问量是最大的,如果您的网站的首页打开的非常缓慢,您的客户将会陆续离开你的网站.通常我们把需要经过复杂运算或者查询数据库得
- 听说有个面试题是: 如何快速向mysql中插入1000w条数据?我私下试了一下, 发现插入10000条数据用了0.9s, 插入10w条数据用
- 如何在pytorch中指定CPU和GPU进行训练,以及cpu和gpu之间切换由CPU切换到GPU,要修改的几个地方:网络模型、损失函数、数据
- 使用axios遇到的相对路径和绝对路径问题部分依赖说明:"vue": "^2.5.2","
- 1、File > Setting > Project:xxx > Project Interpreter 选择或添加环境2
- 表结构如下面代码创建 CREATE TABLE test_tb ( TestId int not null identity(1,1) pr
- JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,它基于ECMAScript的一个子集。 JSON
- 一、前言基于Mediapipe+Opencv实现手势检测,想实现一下姿态识别的时候,感觉手势识别也蛮重要的就过来顺便实现一下。下面是一些国内
- ASP里两种常用的生成文件的方式是:利用ADODB.Stream生成文件和利用Scripting.FileSystemObject(fso)
- 微信小程序 报错:this.setData is not a function在page中定义的代码如下,代码会报错:this.setDat
- 本文实例讲述了Vue开发之封装上传文件组件与用法。分享给大家供大家参考,具体如下:使用elementui的 el-upload插件实现图片上
- 很多DBA目前还停留在Oracle 9i或者10g,究其原因有可能是Oracle 11g的价格问题。本文将为大家讲解在Windows 7下安
- 现在越来越多的浏览器有拦截弹出窗口的功能。广告弹出来给拦掉了就无所谓,要是客户在付款时给拦掉了可就不能乱算了。Gmail的“哎呀”算是经典,
- 一般开发,SQL Server的数据库所有者为dbo.但是为了安全,有时候可能把它换成其它的名称,所有者变换不是很方便.这里列出两种供参考
- 目录完整项目地址:首页安装特点完整项目地址:https://github.com/zsjtoby/DevOpsCloud欢迎使用极云监控系统
- 文档介绍利用python写“猜数字”,“猜词语”,“谁是卧底”这三个游戏,从而快速掌握python编程的入门知识,包括python语法/列表