我在python中创建xml文件时遇到问题。我的脚本在开始时加载一个文本文件,读取每一行并检查内容是否包含特定字符串,具体取决于我想要创建标记或在xml文件中。
为此我正在使用ElementTree
模块。
这个代码
for line in myfile.read().splitlines():
if 'EXTINF' in line:
root = etree.Element("item")
etree.SubElement(root, "title").text = line
elif 'http' in line:
etree.SubElement(root, "link").text = 'http:\\mysite.com\'
tree = etree.ElementTree(root)
tree.write('my\\path\\'+xml_file.xml)
xml文件仅包含for循环迭代的最后一个元素。
我想要的输出是:
<item>
<title> "my title" </title>
<link> "http:\\mysite.com" </link>
</item>
<item>
<title> "my title" </title>
<link> "http:\\mysite.com" </link>
</item>
<item>
<title> "my title" </title>
<link> "http:\\mysite.com" </link>
</item>
怎么了? 感谢
答案 0 :(得分:1)
每次找到root
时,您都会将您分配的元素替换为'EXTINF'
变量。
for line in myfile.read().splitlines():
if 'EXTINF' in line:
root = etree.Element("item") ## Creates a New Element called "item" with a variable name of "root"
etree.SubElement(root, "title").text = line ## Creates a subelement called "title" to the current root variable
elif 'http' in line:
etree.SubElement(root, "link").text = 'http:\\mysite.com\' ## Creates a subelement called "title" to the current root variable
tree = etree.ElementTree(root) ## Create an ElementTree Object using the current root variable
tree.write('my\\path\\'+xml_file.xml)
因此,显然,您需要确定root
变量应该是什么(XML树中的最低节点),然后使用不同的变量来创建root
的子元素。此外,根据您的原始代码和您想要的输出,我假设只输出带有标题的项目,因此您的代码需要反映(具体来说,在您的代码中您只创建{{ 1}}元素如果&#39; EXTINF&#39;在该行中,并且您的示例输出都没有标题。)
item
如果您对没有标题的项目感到满意:
root = etree.Element("root")
for line in myfile.read().splitlines():
if 'EXTINF' in line: ## If it has a title, then create an item.
subelement = etree.SubElement(root,"item")
etree.SubElement(subelement, "title").text = line
if 'http' in line: ## Check if it has a link
etree.SubElement(subelement, "link").text = 'http:\\mysite.com\'
tree = etree.ElementTree(root)
编辑:此外,您是否考虑过使用for line in myfile.read().splitlines():
subelement = etree.SubElement(root,"item") ## Always Create an item for each line
if 'EXTINF' in line:
etree.SubElement(subelement, "title").text = line
if 'http' in line:
etree.SubElement(subelement, "link").text = 'http:\\mysite.com\'
代替myfile.readlines()
?