Python实现读取txt文件并转换为excel的方法示例


Posted in Python onMay 17, 2018

本文实例讲述了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程序设计有所帮助。

Python 相关文章推荐
可用于监控 mysql Master Slave 状态的python代码
Feb 10 Python
python的三目运算符和not in运算符使用示例
Mar 03 Python
使用Python中的cookielib模拟登录网站
Apr 09 Python
python实现逐个读取txt字符并修改
Dec 24 Python
matplotlib.pyplot画图并导出保存的实例
Dec 07 Python
python 递归调用返回None的问题及解决方法
Mar 16 Python
关于python的缩进规则的知识点详解
Jun 22 Python
python如何随机生成高强度密码
Aug 19 Python
python 邮件检测工具mmpi的使用
Jan 04 Python
使用bandit对目标python代码进行安全函数扫描的案例分析
Jan 27 Python
利用Python网络爬虫爬取各大音乐评论的代码
Apr 13 Python
pytorch 梯度NAN异常值的解决方案
Jun 05 Python
cmd运行python文件时对结果进行保存的方法
May 16 #Python
Python基于lxml模块解析html获取页面内所有叶子节点xpath路径功能示例
May 16 #Python
Python使用Dijkstra算法实现求解图中最短路径距离问题详解
May 16 #Python
Python基于Floyd算法求解最短路径距离问题实例详解
May 16 #Python
Python使用selenium实现网页用户名 密码 验证码自动登录功能
May 16 #Python
Selenium 模拟浏览器动态加载页面的实现方法
May 16 #Python
Python selenium实现微博自动登录的示例代码
May 16 #Python
You might like
php模拟socket一次连接,多次发送数据的实现代码
2011/07/26 PHP
php创建多级目录的方法
2015/03/24 PHP
PHP邮箱验证示例教程
2016/06/01 PHP
PHP命令Command模式用法实例分析
2018/08/08 PHP
Prototype源码浅析 String部分(三)之HTML字符串处理
2012/01/15 Javascript
javascript学习(一)构建自己的JS库
2013/01/02 Javascript
JQuery 控制内容长度超出规定长度显示省略号
2014/05/23 Javascript
js实现鼠标点击文本框自动选中内容的方法
2015/08/20 Javascript
JS使用post提交的两种方式
2015/12/03 Javascript
编写高质量JavaScript代码的基本要点
2016/03/02 Javascript
ES6学习笔记之Set和Map数据结构详解
2017/04/07 Javascript
vue+mockjs模拟数据实现前后端分离开发的实例代码
2017/08/08 Javascript
ReactNative 之FlatList使用及踩坑封装总结
2017/11/29 Javascript
微信小程序支付及退款流程详解
2017/11/30 Javascript
BootStrap自定义popover,点击区域隐藏功能的实现
2018/01/23 Javascript
微信小程序利用swiper+css实现购物车商品删除功能
2019/03/06 Javascript
javascript使用substring实现的展开与收缩文字功能示例
2019/06/17 Javascript
Vue +WebSocket + WaveSurferJS 实现H5聊天对话交互的实例
2020/11/18 Vue.js
[05:36]DOTA2 2015国际邀请赛中国区预选赛第四日TOP10
2015/05/29 DOTA
[02:04]2018DOTA2亚洲邀请赛Secret赛前采访
2018/04/03 DOTA
Python实现网站文件的全备份和差异备份
2014/11/30 Python
Python实现Const详解
2015/01/27 Python
python的继承知识点总结
2018/12/10 Python
pytorch permute维度转换方法
2018/12/14 Python
Python实现的矩阵转置与矩阵相乘运算示例
2019/03/26 Python
Django media static外部访问Django中的图片设置教程
2020/04/07 Python
解决matplotlib.pyplot在Jupyter notebook中不显示图像问题
2020/04/22 Python
美国滑雪和滑雪板商店:Buckman
2018/03/03 全球购物
J2EE是技术还是平台还是框架
2016/08/14 面试题
关于毕业的广播稿
2014/01/10 职场文书
生产部管理制度
2014/01/31 职场文书
职业女性的职业规划
2014/03/04 职场文书
毕业生简历自我评价范文
2014/04/09 职场文书
2014年监理工作总结范文
2014/11/17 职场文书
2016年五四青年节校园广播稿
2015/12/17 职场文书
八年级物理教学反思
2016/02/19 职场文书