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 相关文章推荐
python实现排序算法
Feb 14 Python
Python实现基于HTTP文件传输实例
Nov 08 Python
Python合并字符串的3种方法
May 21 Python
python结合opencv实现人脸检测与跟踪
Jun 08 Python
python监控文件或目录变化
Jun 07 Python
Python基于分水岭算法解决走迷宫游戏示例
Sep 26 Python
恢复百度云盘本地误删的文件脚本(简单方法)
Oct 21 Python
Python爬虫工程师面试问题总结
Mar 22 Python
解决Python2.7中IDLE启动没有反应的问题
Nov 30 Python
Python学习笔记之字符串和字符串方法实例详解
Aug 22 Python
使用Python脚本从文件读取数据代码实例
Jan 19 Python
Python制作动态字符画的源码
Aug 04 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 遍历XP文件夹下所有文件
2008/11/27 PHP
php实现统计邮件大小的方法
2013/08/06 PHP
深入理解PHP之OpCode原理详解
2016/06/01 PHP
在多个页面使用同一个HTML片段《续》
2011/03/04 Javascript
页面右下角弹出提示框示例代码js版
2013/08/02 Javascript
写得不错的jquery table鼠标经过变色代码
2013/09/27 Javascript
浅析offsetLeft,Left,clientLeft之间的区别
2013/11/30 Javascript
jquery实现checkbox 全选/全不选的通用写法
2014/02/22 Javascript
js单独获取一个checkbox看其是否被选中
2014/09/22 Javascript
javascript操作数组详解
2014/12/17 Javascript
js如何判断访问是来自搜索引擎(蜘蛛人)还是直接访问
2015/09/14 Javascript
jquery仿京东侧边栏导航效果
2017/03/02 Javascript
Node.js之网络通讯模块实现浅析
2017/04/01 Javascript
jQuery实现鼠标响应式透明度渐变动画效果示例
2018/02/13 jQuery
json对象及数组键值的深度大小写转换问题详解
2018/03/30 Javascript
微信小程序实现默认第一个选中变色效果
2018/07/17 Javascript
vue加载完成后的回调函数方法
2018/09/07 Javascript
JavaScript&quot;模拟事件&quot;的注意要点详解
2019/02/13 Javascript
Vue内部渲染视图的方法
2019/09/02 Javascript
JavaScript RegExp 对象用法详解
2019/09/24 Javascript
python中enumerate函数用法实例分析
2015/05/20 Python
简单易懂的python环境安装教程
2017/07/13 Python
python实现百度语音识别api
2018/04/10 Python
python列表使用实现名字管理系统
2019/01/30 Python
使用Filter过滤python中的日志输出的实现方法
2019/07/17 Python
如何基于Python批量下载音乐
2019/11/11 Python
Python pandas对excel的操作实现示例
2020/07/21 Python
详解Python中的Lock和Rlock
2021/01/26 Python
STAUD官方网站:洛杉矶独有的闲适风格
2019/04/11 全球购物
内容编辑个人求职信
2013/12/10 职场文书
总经理秘书岗位职责
2014/03/17 职场文书
科长竞聘演讲稿
2014/05/16 职场文书
大学毕业典礼演讲稿
2014/09/09 职场文书
2016年国庆节假期旅游工作总结
2016/04/01 职场文书
为什么node.js不适合大型项目
2021/04/28 Javascript
解决golang在import自己的包报错的问题
2021/04/29 Golang