
python将txt文件写入到excel中
with open("111.txt", "r", encoding='utf-8') as f:#打开文本。content = f.read()#读取文本。# Excel 表格的行和列索引从 1 开始。# 创建 Excel 工作簿和工作表。# 将数据写入 Excel 表格。# 读取 txt 文件内容。# 将内容按逗号分隔为列表。# 保存 Excel 文件。
·
import csv
import openpyxl
txt_file = "111.txt"
excel_file = "output_english.xlsx"
# 读取 txt 文件内容
with open("111.txt", "r", encoding='utf-8') as f: #打开文本
content = f.read() #读取文本
# 将内容按逗号分隔为列表
data = content.split("\t")
# 创建 Excel 工作簿和工作表
workbook = openpyxl.Workbook()
worksheet = workbook.active
# 将数据写入 Excel 表格
for i, value in enumerate(data):
# Excel 表格的行和列索引从 1 开始
worksheet.cell(row=i+1, column=1, value=value)
# 保存 Excel 文件
workbook.save(excel_file)
更多推荐
所有评论(0)