gpt4 book ai didi

python - 使用循环将大数据写入 Excel 列单元格

转载 作者:太空宇宙 更新时间:2023-11-03 14:17:20 25 4
gpt4 key购买 nike

解析 .ts 文件并从该文件中获取字符串。我想将该字符串写入 Excel 列。谁能帮助我吗?

from xml.dom import minidom
import sys, xlsxwriter
import pandas as pd
import numpy as np

reload(sys)
sys.setdefaultencoding('utf-8')
doc = minidom.parse("english.ts")


def main():
writer = pd.ExcelWriter('new.xlsx', engine='xlsxwriter')
messages = doc.getElementsByTagName("message")
for message in messages:
source = message.getElementsByTagName("source")[0]
ori_string = source.firstChild.data
print ori_string
df = pd.DataFrame({'TString': [ori_string]})
writer = pd.ExcelWriter('new.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1')
writer.save()


if __name__ == '__main__':
main()

我没有收到错误,并且“ori_string”正在打印,但在 Excel 工作表中没有写入任何内容。

我只想将字符串写入 Excel 列。如何迭代行以及如何在迭代内给出行号和列号?

最佳答案

from xml.dom import minidom
import sys, xlsxwriter

reload(sys)
sys.setdefaultencoding('utf-8')
doc = minidom.parse("english.ts")


def main():
row = 0
messages = doc.getElementsByTagName("message")
workbook = xlsxwriter.Workbook('data3.xlsx')
worksheet = workbook.add_worksheet()
for message in messages:
source = message.getElementsByTagName("source")[0]
ori_string = source.firstChild.data
print ori_string
worksheet.write(row, 0, ori_string)
row += 1
workbook.close()

Now its working thank you @DavidG for your suggestion.

关于python - 使用循环将大数据写入 Excel 列单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48164969/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com