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异常处理慎用“except:pass”建议
Apr 02 Python
在Python中使用HTMLParser解析HTML的教程
Apr 29 Python
利用python写个下载teahour音频的小脚本
May 08 Python
简单学习Python多进程Multiprocessing
Aug 29 Python
python针对excel的操作技巧
Mar 13 Python
python中bs4.BeautifulSoup的基本用法
Jul 27 Python
Python爬虫图片懒加载技术 selenium和PhantomJS解析
Sep 18 Python
Pycharm远程连接服务器并实现代码同步上传更新功能
Feb 25 Python
Django基于Models定制Admin后台实现过程解析
Nov 11 Python
Python实战之用tkinter库做一个鼠标模拟点击器
Apr 27 Python
Django实现聊天机器人
May 31 Python
Django实现WebSocket在线聊天室功能(channels库)
Sep 25 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安全之直接用$获取值而不$_GET 字符转义
2012/06/03 PHP
php抽奖小程序的实现代码
2013/06/18 PHP
使用淘宝IP库获取用户ip地理位置
2013/10/27 PHP
学习php设计模式 php实现门面模式(Facade)
2015/12/07 PHP
PHP实现统计代码行数小工具
2019/09/19 PHP
javascript网页关闭时提醒效果脚本
2008/10/22 Javascript
jQuery动态添加的元素绑定事件处理函数代码
2011/08/02 Javascript
jquery无法设置checkbox选中即没有变成选中状态
2014/03/27 Javascript
Js Jquery创建一个弹出层可加载一个页面
2014/05/08 Javascript
JS实现转动随机数抽奖特效代码
2020/04/16 Javascript
解决微信浏览器Javascript无法使用window.location.reload()刷新页面
2016/06/21 Javascript
leaflet的开发入门教程
2016/11/17 Javascript
Vuex之理解Getters的用法实例
2017/04/19 Javascript
js定时器实现倒计时效果
2017/11/05 Javascript
浅析Vue自定义组件的v-model
2017/11/26 Javascript
Vue中使用webpack别名的方法实例详解
2018/06/19 Javascript
在 Vue 中编写 SVG 图标组件的方法
2020/02/24 Javascript
JS中的const命令你真懂它吗
2020/03/08 Javascript
vue中touch和click共存的解决方式
2020/07/28 Javascript
Vue获取微博授权URL代码实例
2020/11/04 Javascript
[01:09:50]VP vs Pain 2018国际邀请赛小组赛BO2 第二场
2018/08/20 DOTA
python的迭代器与生成器实例详解
2014/07/16 Python
python连接远程ftp服务器并列出目录下文件的方法
2015/04/01 Python
Python遍历目录并批量更换文件名和目录名的方法
2016/09/19 Python
Python在不同目录下导入模块的实现方法
2017/10/27 Python
python使用pymongo与MongoDB基本交互操作示例
2020/04/09 Python
Python 实现键盘鼠标按键模拟
2020/11/18 Python
挪威手表购物网站:Klokker
2016/09/19 全球购物
平面设计师工作职责范文
2013/12/03 职场文书
个人自荐书
2013/12/20 职场文书
2014学雷锋活动心得体会
2014/03/10 职场文书
道德之星事迹材料
2014/05/03 职场文书
商务代表岗位职责
2015/02/15 职场文书
师德承诺书2015
2015/04/28 职场文书
建国大业观后感
2015/06/01 职场文书
PYTHON 使用 Pandas 删除某列指定值所在的行
2022/04/28 Python