尝试将列表内容写入 xml
Trying to write contents of lists into xml
我有 3 个长度相同的列表,我正在尝试将它们写入 xml 文件,如下所示:
import xml.etree.ElementTree as ET
import re
title_list = ['title1', 'title2', 'title3']
date_list = ['date1', 'date2', 'date3']
text_list = ['text1', 'text2', 'text3']
book = ET.Element('book')
for e in zip(title_list, date_list, text_list):
book = ET.Element('book')
article = ET.SubElement(book, 'article')
title = ET.SubElement(article, 'title')
date = ET.SubElement(article, 'date')
text = ET.SubElement(article, 'text')
title.text = e[0]
date.text = e[1]
text.text = e[2]
tree_out = ET.ElementTree(book)
tree_out.write('test.xml')
我的输出('pretty printed' 在氧气中):
<book>
<article>
<title>title3</title>
<date>date3</date>
<text>text3</text>
</article>
</book>
但我想要:
<book>
<article>
<title>title1</title>
<date>date1</date>
<text>text1</text>
</article>
<article>
<title>title2</title>
<date>date2</date>
<text>text2</text>
</article>
<article>
<title>title3</title>
<date>date3</date>
<text>text3</text>
</article>
</book>
如何获得整个输出?我在这里读了很多书,但对我没有任何用处。请用简单的术语解释一下,因为我对这个库还很陌生,一般来说 xml
在你的 for
循环中,你重新定义了 book
所以它会被每次迭代覆盖。
删除它,您应该会得到想要的结果...
import xml.etree.ElementTree as ET
title_list = ['title1', 'title2', 'title3']
date_list = ['date1', 'date2', 'date3']
text_list = ['text1', 'text2', 'text3']
book = ET.Element('book')
for e in zip(title_list, date_list, text_list):
# book = ET.Element('book') # <-- REMOVE THIS LINE
article = ET.SubElement(book, 'article')
title = ET.SubElement(article, 'title')
date = ET.SubElement(article, 'date')
text = ET.SubElement(article, 'text')
title.text = e[0]
date.text = e[1]
text.text = e[2]
tree_out = ET.ElementTree(book)
tree_out.write('test.xml')
我有 3 个长度相同的列表,我正在尝试将它们写入 xml 文件,如下所示:
import xml.etree.ElementTree as ET
import re
title_list = ['title1', 'title2', 'title3']
date_list = ['date1', 'date2', 'date3']
text_list = ['text1', 'text2', 'text3']
book = ET.Element('book')
for e in zip(title_list, date_list, text_list):
book = ET.Element('book')
article = ET.SubElement(book, 'article')
title = ET.SubElement(article, 'title')
date = ET.SubElement(article, 'date')
text = ET.SubElement(article, 'text')
title.text = e[0]
date.text = e[1]
text.text = e[2]
tree_out = ET.ElementTree(book)
tree_out.write('test.xml')
我的输出('pretty printed' 在氧气中):
<book>
<article>
<title>title3</title>
<date>date3</date>
<text>text3</text>
</article>
</book>
但我想要:
<book>
<article>
<title>title1</title>
<date>date1</date>
<text>text1</text>
</article>
<article>
<title>title2</title>
<date>date2</date>
<text>text2</text>
</article>
<article>
<title>title3</title>
<date>date3</date>
<text>text3</text>
</article>
</book>
如何获得整个输出?我在这里读了很多书,但对我没有任何用处。请用简单的术语解释一下,因为我对这个库还很陌生,一般来说 xml
在你的 for
循环中,你重新定义了 book
所以它会被每次迭代覆盖。
删除它,您应该会得到想要的结果...
import xml.etree.ElementTree as ET
title_list = ['title1', 'title2', 'title3']
date_list = ['date1', 'date2', 'date3']
text_list = ['text1', 'text2', 'text3']
book = ET.Element('book')
for e in zip(title_list, date_list, text_list):
# book = ET.Element('book') # <-- REMOVE THIS LINE
article = ET.SubElement(book, 'article')
title = ET.SubElement(article, 'title')
date = ET.SubElement(article, 'date')
text = ET.SubElement(article, 'text')
title.text = e[0]
date.text = e[1]
text.text = e[2]
tree_out = ET.ElementTree(book)
tree_out.write('test.xml')