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编程开发之日期操作实例分析
Nov 13 Python
Python2/3中urllib库的一些常见用法
Dec 19 Python
Python结合ImageMagick实现多张图片合并为一个pdf文件的方法
Apr 24 Python
使用pandas read_table读取csv文件的方法
Jul 04 Python
python中selenium操作下拉滚动条的几种方法汇总
Jul 14 Python
Python 仅获取响应头, 不获取实体的实例
Aug 21 Python
python实现生成Word、docx文件的方法分析
Aug 30 Python
利用python实现冒泡排序算法实例代码
Dec 01 Python
PYcharm 激活方法(推荐)
Mar 23 Python
简单的命令查看安装的python版本号
Aug 28 Python
python如何发送带有附件、正文为HTML的邮件
Feb 27 Python
详解Python内置模块Collections
Mar 22 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
pw的一个放后门的方法分析
2007/10/08 PHP
网友原创的PHP模板类代码
2008/09/07 PHP
用PHP实现弹出消息提示框的两种方法
2013/12/17 PHP
PHP使用glob函数遍历目录或文件夹的方法
2014/12/16 PHP
完整删除ecshop中获取店铺信息的API
2014/12/24 PHP
PHP中is_file()函数使用指南
2015/05/08 PHP
PhpStorm+xdebug+postman调试技巧分享
2020/09/15 PHP
JQuery Ajax 跨域访问的解决方案
2010/03/12 Javascript
JS 跳转页面延迟2种方法
2013/03/29 Javascript
ie8模式下click无反应点击option无反应的解决方法
2014/10/11 Javascript
node.js中Socket.IO的进阶使用技巧
2014/11/04 Javascript
12种JavaScript常用的MVC框架比较分析
2015/11/16 Javascript
基于BootStarp的Dailog
2016/04/28 Javascript
js判断登陆用户名及密码是否为空的简单实例
2016/05/16 Javascript
全面解析JavaScript中apply和call以及bind(推荐)
2016/06/15 Javascript
D3.js实现散点图和气泡图的方法详解
2016/09/21 Javascript
JS+HTML5实现的前端购物车功能插件实例【附demo源码下载】
2016/10/17 Javascript
基于Vue.js实现tab滑块效果
2017/07/23 Javascript
[06:49]2018DOTA2国际邀请赛寻真——VirtusPro傲视群雄
2018/08/12 DOTA
[59:42]Secret vs Alliacne 2019国际邀请赛小组赛 BO2 第一场 8.15
2019/08/17 DOTA
Python实现爬虫从网络上下载文档的实例代码
2018/06/13 Python
python3爬虫怎样构建请求header
2018/12/23 Python
Python基于聚类算法实现密度聚类(DBSCAN)计算【测试可用】
2018/12/26 Python
使用TensorFlow实现二分类的方法示例
2019/02/05 Python
python交易记录整合交易类详解
2019/07/03 Python
python 魔法函数实例及解析
2019/09/25 Python
Python字符串、列表、元组、字典、集合的补充实例详解
2019/12/20 Python
Python request使用方法及问题总结
2020/04/26 Python
Python DES加密实现原理及实例解析
2020/07/17 Python
平面设计自荐信
2013/10/07 职场文书
工伤赔偿协议书
2014/04/15 职场文书
2014年小学辅导员工作总结
2014/12/23 职场文书
社区服务活动报告
2015/02/05 职场文书
专项资金申请报告
2015/05/15 职场文书
Python-OpenCV教程之图像的位运算详解
2021/06/21 Python
SQLServer RANK() 排名函数的使用
2022/03/23 SQL Server