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 相关文章推荐
编写自定义的Django模板加载器的简单示例
Jul 21 Python
django模板语法学习之include示例详解
Dec 17 Python
Python cookbook(数据结构与算法)将序列分解为单独变量的方法
Feb 13 Python
用tensorflow搭建CNN的方法
Mar 05 Python
单利模式及python实现方式详解
Mar 20 Python
numpy.transpose对三维数组的转置方法
Apr 17 Python
对Python random模块打乱数组顺序的实例讲解
Nov 08 Python
pandas计数 value_counts()的使用
Jun 24 Python
python自动化测试无法启动谷歌浏览器问题
Oct 10 Python
python飞机大战pygame游戏框架搭建操作详解
Dec 17 Python
python 5个实用的技巧
Sep 27 Python
Python+Appium实现自动化清理微信僵尸好友的方法
Feb 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 前一天或后一天的日期
2008/06/28 PHP
PHP中实现汉字转区位码应用源码实例解析
2010/06/14 PHP
php判断字符串在另一个字符串位置的方法
2014/02/27 PHP
浅谈PHP调用Webservice思路及源码分享
2014/06/04 PHP
关于javascript function对象那些迷惑分析
2011/10/24 Javascript
Js获取数组最大和最小值示例代码
2013/10/29 Javascript
让新消息在网页标题闪烁提示的jQuery代码
2013/11/04 Javascript
表单验证正则表达式实例代码详解
2015/11/09 Javascript
jQuery.ajax实现根据不同的Content-Type做出不同的响应
2016/11/03 Javascript
nodejs body-parser 解析post数据实例
2017/07/26 NodeJs
Angular js 实现添加用户、修改密码、敏感字、下拉菜单的综合操作方法
2017/10/24 Javascript
jQuery动态添加元素无法触发绑定事件的解决方法分析
2018/01/02 jQuery
angularjs 获取默认选中的单选按钮的value方法
2018/02/28 Javascript
nodejs前端模板引擎swig入门详解
2018/05/15 NodeJs
vue ssr 指南详读
2018/06/29 Javascript
微信小程序实现九宫格抽奖
2020/04/15 Javascript
vue实现路由懒加载及组件懒加载的方式
2019/06/11 Javascript
vue 使用 canvas 实现手写电子签名
2020/03/06 Javascript
vue实践---根据不同环境,自动转换请求的url地址操作
2020/09/21 Javascript
使用Python脚本操作MongoDB的教程
2015/04/16 Python
Python实现的下载网页源码功能示例
2017/06/13 Python
Python模拟登陆实现代码
2017/06/14 Python
Python3使用PyQt5制作简单的画板/手写板实例
2017/10/19 Python
在python image 中安装中文字体的实现方法
2019/08/22 Python
Python 网络编程之UDP发送接收数据功能示例【基于socket套接字】
2019/10/11 Python
意大利和国际最佳时尚品牌:Drestige
2019/12/28 全球购物
在线实验室测试:HealthLabs.com
2020/05/03 全球购物
外语系毕业生自荐信范文
2013/12/16 职场文书
课例研修方案
2014/05/31 职场文书
求职简历自荐信
2014/06/18 职场文书
新疆民族团结演讲稿
2014/08/27 职场文书
关于感谢信的范文
2015/01/23 职场文书
2015年助残日活动总结
2015/03/27 职场文书
2017寒假社会实践心得体会范文
2016/01/14 职场文书
复制别人的成功真的会成功吗?
2019/10/17 职场文书
导游词之舟山普陀山
2019/11/06 职场文书