本文实现用Python将文本文件自动保存到Excel表格里面去。
需求
- 将锦江区.txt 文件中的数据整理到 锦江区.xlsx 的 锦江区 sheet ;
- 将推荐菜字段丢弃(保留前面14个字段) ;
- 将人均消费中的 ¥ 符号去掉,如果价格为空整条数据都不要;
成果展示
txt文本部分数据
效果
玩一玩,表现出对他人的尊重。
[En]
Play a little bit, show some respect for others.
; 代码展示
import openpyxl
Python学习交流群 815624229
workbook = openpyxl.Workbook()
sheet = workbook.active
sheet.title = '锦江区'
平均价格在列表中的序号
price_no = 6
with open('锦江区.txt', mode='r', encoding='utf-8') as f:
header = f.readline()
lines = f.readlines()
header = header.split(',')[:14]
sheet.append(header)
print(header)
for line in lines:
line_data = line.split(',')[:14]
if not line_data[price_no]:
continue
line_data[price_no] = line_data[price_no].strip('¥')
sheet.append(line_data)
workbook.save('锦江区.xlsx')
好了,伙计们,去试试吧!记得要喜欢这个收藏~
[En]
All right, guys, go and try it! Remember to like the collection ~
Original: https://www.cnblogs.com/hahaa/p/16167257.html
Author: 轻松学Python
Title: Python实现将文本数据批量导入Excel表格,并按格式保存
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/499874/
转载文章受原作者版权保护。转载请注明原作者出处!