我有一个 txt 文件包含超过 10 万行,并且我想为每一行创建一个 XML 树。但是所有行都共享同一个根。
这里的txt文件:
LIBRARY:
1,1,1,1,the
1,2,1,1,world
2,1,1,2,we
2,5,2,1,have
7,3,1,1,food
所需的输出:
<LIBRARY>
<BOOK ID ="1">
<CHAPTER ID ="1">
<SENT ID ="1">
<WORD ID ="1">the</WORD>
</SENT>
</CHAPTER>
</BOOK>
<BOOK ID ="1">
<CHAPTER ID ="2">
<SENT ID ="1">
<WORD ID ="1">world</WORD>
</SENT>
</CHAPTER>
</BOOK>
<BOOK ID ="2">
<CHAPTER ID ="1">
<SENT ID ="1">
<WORD ID ="2">we</WORD>
</SENT>
</CHAPTER>
</BOOK>
<BOOK ID ="2">
<CHAPTER ID ="5">
<SENT ID ="2">
<WORD ID ="1">have</WORD>
</SENT>
</CHAPTER>
</BOOK>
<BOOK ID ="7">
<CHAPTER ID ="3">
<SENT ID ="1">
<WORD ID ="1">food</WORD>
</SENT>
</CHAPTER>
</BOOK>
</LIBRARY>
我使用元素树将 txt 文件转换为 xml 文件,这是我运行的代码
def expantree():
lines = txtfile.readlines()
for line in lines:
split_line = line.split(',')
BOOK.set( 'ID ', split_line[0])
CHAPTER.set( 'ID ', split_line[1])
SENTENCE.set( 'ID ', split_line[2])
WORD.set( 'ID ', split_line[3])
WORD.text = split_line[4]
tree = ET.ElementTree(Root)
tree.write(xmlfile)
好的,代码正在运行,但我没有得到想要的输出,我得到了以下内容:
<LIBRARY>
<BOOK ID ="1">
<CHAPTER ID ="1">
<SENT ID ="1">
<WORD ID ="1">the</WORD>
</SENT>
</CHAPTER>
</BOOK>
</LIBRARY>
<LIBRARY>
<BOOK ID ="1">
<CHAPTER ID ="2">
<SENT ID ="1">
<WORD ID ="1">world</WORD>
</SENT>
</CHAPTER>
</BOOK>
</LIBRARY>
<LIBRARY>
<BOOK ID ="2">
<CHAPTER ID ="1">
<SENT ID ="1">
<WORD ID ="2">we</WORD>
</SENT>
</CHAPTER>
</BOOK>
</LIBRARY>
<LIBRARY>
<BOOK ID ="2">
<CHAPTER ID ="5">
<SENT ID ="2">
<WORD ID ="1">have</WORD>
</SENT>
</CHAPTER>
</BOOK>
</LIBRARY>
<LIBRARY>
<BOOK ID ="7">
<CHAPTER ID ="3">
<SENT ID ="1">
<WORD ID ="1">food</WORD>
</SENT>
</CHAPTER>
</BOOK>
</LIBRARY>
如何统一树根,而不是获得许多根标签,我得到一个根标签?