怎么用python将有格式的txt文档读入excel文档当中

即用分隔符,换行符预先设定好的txt文档,手动copy到excel中会生成一个excel表格(有时需要重选分隔符)。

那么这个功能(从打开指定TXT文档到生成表格)用python如何实现能?需要用到那些扩展包呢?

例如,txt文本中有以下带有格式的内容:
Marketplace,Rule Priority,Rule Name,Rule Status,First Launch Date,Age 0-0.5,Age 0.5-1,Age 1-2,Age 2-3,Age 3+,Total
JP, 3, JP_Weapons_Knives_Blade_6_15, LAUNCHED, 2018-09-03T01:35:44, 42, 6, 2, 40, 18, 108

2个回答

from openpyxl import Workbook

title = "Marketplace,Rule Priority,Rule Name,Rule Status,First Launch Date,Age 0-0.5,Age 0.5-1,Age 1-2,Age 2-3,Age 3+,Total"
data = "JP, 3, JP_Weapons_Knives_Blade_6_15, LAUNCHED, 2018-09-03T01:35:44, 42, 6, 2, 40, 18, 108"
title_list = title.split(",")
data_list = data.split(",")

wb = Workbook()
# 创建一个sheet
ws = wb.create_sheet()
ws.append(title_list)
ws.append(data_list)

wb.save(r'demo.xlsx')
congcongmini
yueconger 回复Ryo.E: 还是看具体需求吧,读取是第一步,后面如何对应列表索引可以自己 设计
12 个月之前 回复
weixin_43960795
Ryo.E 如果data有很多行,怎么自动选定全部呢?
大约一年之前 回复

from openpyxl import Workbook

title = "Marketplace,Rule Priority,Rule Name,Rule Status,First Launch Date,Age 0-0.5,Age 0.5-1,Age 1-2,Age 2-3,Age 3+,Total"
data = "JP, 3, JP_Weapons_Knives_Blade_6_15, LAUNCHED, 2018-09-03T01:35:44, 42, 6, 2, 40, 18, 108"
title_list = title.split(",")
data_list = data.split(",")
wb = Workbook()
ws = wb.create_sheet()
ws.append(title_list)
ws.append(data_list)
wb.save(r'demo.xlsx')

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问