python读取txt文件并写入excel-Python实现读取txt文件并转换为excel的方法示例
本文实例讲述了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读取txt文件并写入excel-Python实现读取txt文件并转换为excel的方法示例相关推荐
- python数字转中文字符_Python实现中文数字转换为阿拉伯数字的方法示例
本文实例讲述了Python实现中文数字转换为阿拉伯数字的方法.分享给大家供大家参考,具体如下: 一.需求 今天写了三千二百行代码. 今天写了3200行代码. 两行意思相同,只是表达方式不太能够,统一掉 ...
- python创建txt文件并写入 编码-Python文本文件读写操作时的字符编码问题
说明1:文本文件的字符编码问题只存在t模式中,如:open('a.txt', mode='rt') 编码(encode): 我们输入的任何字符想要以文件(如.txt)的形式保存在计算机的硬盘上, 必须 ...
- python数字转换_Python实现中文数字转换为阿拉伯数字的方法示例
本文实例讲述了Python实现中文数字转换为阿拉伯数字的方法.分享给大家供大家参考,具体如下: 一.需求 今天写了三千二百行代码. 今天写了3200行代码. 两行意思相同,只是表达方式不太能够,统一掉 ...
- python中字符串转xml对象_Python实现对象转换为xml的方法示例
本文实例讲述了Python实现对象转换为xml的方法.分享给大家供大家参考,具体如下: # -*- coding:UTF-8 -*- ''''' Created on 2010-4-20 @autho ...
- php转换excel文件怎么打开方式,用PHP将mysql数据表转换为excel文件格式_php
详细内容如下: $DB_Server = "localhost"; $DB_Username = "mydowns"; $DB_Password = " ...
- python 创建txt文件并写入字符串-python创建txt文件
1.自己写入txt 直接上核心代码:with open("douban.txt","w") as f: f.write("这是个测试!")1 ...
- python 创建txt文件并写入字符串-Python将字节字符串写入文件
How do I write a string of bytes to a file, in byte mode, using python? I have: ['0x28', '0x0', '0x0 ...
- java 导出txt,java生成txt,并写入内容,java读取txt文本内容
public class ReadFromFile {/*** 以字节为单位读取文件,常用于读二进制文件,如图片.声音.影像等文件.*/public static void readFileByByt ...
- python读取gif图片并显示_Python爬虫实现获取动态gif格式搞笑图片的方法示例
本文实例讲述了python爬虫实现获取动态gif格式搞笑图片的方法.分享给大家供大家参考,具体如下: 有时候看到一些喜欢的动图,如果一个个取保存挺麻烦,有的网站还不支持右键保存,因此使用python来 ...
- python爬取新闻网站内容findall函数_Python正则抓取新闻标题和链接的方法示例
本文实例讲述了Python正则抓取新闻标题和链接的方法.分享给大家供大家参考,具体如下: #-*-coding:utf-8-*- import re from urllib import urlret ...
最新文章
- 基于SSH实现在线课程学习系统
- Tomcat在修改代码后不会自动reload解决办法
- AngularJS+RequireJS集成环境
- GC回收垃圾对象过程
- oracle u01清理,Oracleの/u01/11g/diag/rdbms/orcl/orcl/incident 的清理
- java addcallback函数_java中怎么使用callback函数?
- 循环比赛日程表(信息学奥赛一本通-T1325)
- 笨方法“学习python笔记之条件控制
- Python与开源GIS:GDAL简介转载
- Python+sklearn使用线性回归算法预测儿童身高
- C语言-数组练习题(附答案)
- c语言编程运行符号是什么,c语言编程用的符号有哪些
- ADB各种操作指令详解大汇总
- Springboot2.2对put,detele方法的更改
- windows移动硬盘接到mac上使用(加载ntfs移动硬盘)
- 基于Python+网络爬虫的兼职招聘就业信息数据可视化分析
- 今天分享一个Python游戏:简约飞机模拟器
- 中冠百年|年轻人收入低如何理财
- sift特征检测与匹配
- collections.abc与abc模块是同一个东西吗?