所以我还在学习如何使用xml.etree.ElementTree来创建xml文档。我想把字符串的句子写成xml文件。
这是我的代码:
import sys
import os
import re
import xml.etree.ElementTree as ET
input_f = "test.txt"
output_f = "results.txt"
lines = open(input_f, "r").readlines()
sentences = [x.strip() for x in ' '.join(lines).replace("\n","").split(".")]
with open(output_f,"w") as f:
for sentence in sentences:
if sentence != "":
f.write("%s\n\n" % sentence)
xml_list = []
#convert sentences to xml
for line in output_f:
event_descri = ET.fromstring(line, parser=None)
xml_list.append(event_descri)
# writes to xml file
with open("chronology.xml", "w") as f:
f.write(ET.tostring(xml_list))
这是我的错误:
File "C:\Python34\lib\xml\etree\ElementTree.py", line 1326, in XML
return parser.close()
File "<string>", line None
xml.etree.ElementTree.ParseError: syntax error: line 1, column 0
这里发生了什么?有人可以帮忙吗?