Python实现读取txt文件并转换为excel的方法示例

作者:江枫渔火2017 时间:2023-07-25 15:20:16 

本文实例讲述了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程序设计有所帮助。

来源:https://blog.csdn.net/Jesse__Zhong/article/details/79690617

标签:Python,txt,excel
0
投稿

猜你喜欢

  • Python 中如何写注释

    2022-07-10 21:53:11
  • OpenCV结合selenium实现滑块验证码

    2022-06-29 14:18:57
  • Python写出新冠状病毒确诊人数地图的方法

    2022-09-28 06:15:13
  • Python Flask异步发送邮件实现方法解析

    2023-08-15 04:29:12
  • 详述numpy中的np.random.random()系列函数用法

    2023-08-08 00:25:09
  • Pytorch相关知识介绍与应用

    2021-11-14 13:37:57
  • Node.js使用NodeMailer发送邮件实例代码

    2024-05-02 17:37:10
  • Python标准库之数据库 sqlite3

    2024-01-24 01:50:51
  • 详解Python文件修改的两种方式

    2023-03-14 13:44:13
  • 关于JS中变量的显式申明和隐式申明

    2008-09-12 13:04:00
  • ASP中DLL的调试环境配置全攻略

    2007-09-27 13:20:00
  • Flask中嵌套启动子线程的方法示例详解

    2023-01-13 21:13:08
  • 详解Python如何实现Excel数据读取和写入

    2023-10-29 06:46:35
  • pycharm快捷键汇总

    2022-12-30 22:43:40
  • 用Python进行一些简单的自然语言处理的教程

    2021-07-03 19:51:26
  • python实现linux下使用xcopy的方法

    2022-08-16 07:52:09
  • Django模板Templates使用方法详解

    2023-04-22 07:40:47
  • vue实现PC端分辨率适配操作

    2024-05-09 09:29:57
  • python读取浮点数和读取文本文件示例

    2022-10-31 16:03:38
  • JS实战篇之收缩菜单表单布局

    2024-04-18 09:47:43
  • asp之家 网络编程 m.aspxhome.com