Python读取txt内容写入xls格式excel中的方法
作者:WeilianSunshine 时间:2023-08-31 22:29:17
由于xlwt目前只支持xls格式,至于xlsx格式,后面会继续更新
import xlwt
import codecs
def Txt_to_Excel(inputTxt,sheetName,start_row,start_col,outputExcel):
fr = codecs.open(inputTxt,'r')
wb = xlwt.Workbook(encoding = 'utf-8')
ws = wb.add_sheet(sheetName)
line_number = 0#记录有多少行,相当于写入excel时的i,
row_excel = start_row
try:
for line in fr :
line_number +=1
row_excel +=1
line = line.strip()
line = line.split(' ')
len_line = len(line)#list中每一行有多少个数,相当于写入excel中的j
col_excel = start_col
for j in range(len_line):
print (line[j])
ws.write(row_excel,col_excel,line[j])
col_excel +=1
wb.save(outputExcel)
except:
print ('')
if __name__=='__main__':
sheetName = 'Sheet2'#需要写入excel中的Sheet2中,可以自己设定
start_row = 7 #从第7行开始写
start_col = 3 #从第3列开始写
inputfile = 'text.txt' #输入文件
outputExcel = 'excel_result.xls' #输出excel文件
Txt_to_Excel(inputfile,sheetName,start_row,start_col,outputExcel)el)
来源:https://blog.csdn.net/qq_24471569/article/details/76713795
标签:Python,txt,xls,excel
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python爬虫中采集中遇到的问题整理
2022-10-17 03:32:23
详细介绍ASP内置对象Response
2008-06-23 12:42:00
巧用JDBC实现对MySQL的“增删改查”
2008-12-31 15:12:00
SQLite3中文编码 Python的实现
2023-02-03 13:21:38
解决Tensorflow安装成功,但在导入时报错的问题
2022-03-05 18:39:27
pytorch如何获得模型的计算量和参数量
2023-07-24 06:44:43
![](https://img.aspxhome.com/file/2023/1/124181_0s.png)
Python 处理数据的实例详解
2021-05-31 21:29:35
asp函数InstrRev()介绍及获取文件名例子
2007-11-19 19:01:00
Python实现按特定格式对文件进行读写的方法示例
2022-06-07 22:59:57
![](https://img.aspxhome.com/file/2023/8/118758_0s.png)
基于Python的接口自动化读写excel文件的方法
2023-09-04 00:25:10
![](https://img.aspxhome.com/file/2023/0/93970_0s.png)
Python List remove()实例用法详解
2022-11-24 17:40:18
解决python虚拟环境切换无效的问题
2023-02-01 14:37:50
![](https://img.aspxhome.com/file/2023/0/77860_0s.jpg)
简单瞅瞅Python vars()内置函数的实现
2021-03-29 20:41:05
UTF-8 编码中BOM的检测与删除
2022-06-04 07:44:16
NumPy实现多维数组中的线性代数
2021-03-30 09:59:08
![](https://img.aspxhome.com/file/2023/8/89418_0s.png)
go语言beego框架web开发语法笔记示例
2024-05-21 10:25:22
Spring Boot集成MyBatis访问数据库的方法
2024-01-23 06:45:43
Python实现找到同名文件并复制到其他文件夹中
2022-03-16 21:53:14
![](https://img.aspxhome.com/file/2023/8/113138_0s.png)
PHP根据key删除数组中指定的元素
2023-07-17 19:46:27
在Django中管理Users和Permissions以及Groups的方法
2023-11-03 03:10:21