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实现的简单文本类游戏实例
Apr 28 Python
Python处理JSON时的值报错及编码报错的两则解决实录
Jun 26 Python
Python编程实现及时获取新邮件的方法示例
Aug 10 Python
详解基于python-django框架的支付宝支付案例
Sep 23 Python
Python FTP文件定时自动下载实现过程解析
Nov 12 Python
利用matplotlib实现根据实时数据动态更新图形
Dec 13 Python
Python 使用 prettytable 库打印表格美化输出功能
Dec 26 Python
Django models filter筛选条件详解
Mar 16 Python
django实现更改数据库某个字段以及字段段内数据
Mar 31 Python
matplotlib 三维图表绘制方法简介
Sep 20 Python
python中time.ctime()实例用法
Feb 03 Python
Python中else的三种使用场景
Jun 16 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
Laravel 4.2 中队列服务(queue)使用感受
2014/10/30 PHP
php中FTP函数ftp_connect、ftp_login与ftp_chmod用法
2014/11/18 PHP
Lazy Load 延迟加载图片的 jQuery 插件
2010/02/06 Javascript
为jQuery.Treeview添加右键菜单的实现代码
2010/10/22 Javascript
jQueryUI写一个调整分类的拖放效果实现代码
2012/05/10 Javascript
jquery解析xml字符串示例分享
2014/03/25 Javascript
使用jquery实现放大镜效果
2014/09/02 Javascript
jQuery插件datalist实现很好看的input下拉列表
2015/07/14 Javascript
jquery实现的Banner广告收缩效果代码
2015/09/02 Javascript
Node.js文件操作方法汇总
2016/03/22 Javascript
修改js confirm alert 提示框文字的简单实例
2016/06/10 Javascript
AngularJs $parse、$eval和$observe、$watch详解
2016/09/21 Javascript
JavaScript函数基础详解
2017/02/03 Javascript
Vue.js中的computed工作原理
2018/03/22 Javascript
Angular开发实践之服务端渲染
2018/03/29 Javascript
如何使node也支持从url加载一个module详解
2018/06/05 Javascript
VUE接入腾讯验证码功能(滑块验证)备忘
2019/05/07 Javascript
[03:36]DOTA2完美大师赛coL战队趣味视频——我演你猜
2017/11/23 DOTA
[49:27]2018DOTA2亚洲邀请赛 4.4 淘汰赛 TNC vs VG 第一场
2018/04/05 DOTA
Django框架下在URLconf中指定视图缓存的方法
2015/07/23 Python
Kali Linux安装ipython2 和 ipython3的方法
2019/07/11 Python
python自动生成model文件过程详解
2019/11/02 Python
python连接PostgreSQL过程解析
2020/02/09 Python
python使用matplotlib绘制折线图的示例代码
2020/09/22 Python
伦敦一家非常流行的时尚精品店:Oxygen Boutique
2017/01/15 全球购物
西班牙Polo衫品牌:Polo Club
2020/08/09 全球购物
计算机通信工程专业毕业生推荐信
2013/12/24 职场文书
学生党员思想汇报范文
2014/01/09 职场文书
服务宗旨标语
2014/07/01 职场文书
演讲比赛的活动方案
2014/08/28 职场文书
二手房购房协议书范本
2014/10/05 职场文书
儿园租房协议书范本
2014/12/02 职场文书
世界卫生日宣传活动总结
2015/02/09 职场文书
Python道路车道线检测的实现
2021/06/27 Python
python字典的元素访问实例详解
2021/07/21 Python
Java实现字符串转为驼峰格式的方法详解
2022/07/07 Java/Android