解析.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 column.how来迭代行以及如何在迭代中给出行号和列号?
答案 0 :(得分:1)
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()
现在它正在努力感谢@DavidG的建议。