[英]Python and xml.etree.ElementTree -
I got a big xml-file with the following structure:我得到了一个具有以下结构的大 xml 文件:
<defaults>
<options>
<param name="email">email@email.org</param>
<param name="email">email2@email.org</param>
<param name="email">email3@email.org</param>
</options>
</defaults>
I use xml.etree.ElementTree as ET Lib with the following code:我使用 xml.etree.ElementTree 作为 ET Lib 和以下代码:
tree = ET.parse("xml_file.xml")
root = tree.getroot()
for admin in root.iter("param"):
mail = admin.text("name")
print(mail)
With the code above I ge the param "classes", I need the content (email) between the param-tags.使用上面的代码,我得到了参数“类”,我需要参数标签之间的内容(电子邮件)。
Andy ideas?安迪的想法?
Trying to run your snippet I get试图运行你的代码片段我得到
TypeError: 'str' object is not callable
类型错误:“str”对象不可调用
Because .text
is an attribute which holds the "text content" of the node.因为
.text
是一个保存节点“文本内容”的属性。
for admin in root.iter("param"):
print(admin.text)
generates the expected:产生预期:
email@email.org
email2@email.org
email3@email.org
I've no idea what "the param "classes"" would even be, there's no "classes" anywhere in what you provide.我什至不知道“参数“类”是什么,您提供的任何地方都没有“类”。
See the code below看下面的代码
import xml.etree.ElementTree as ET
tree = ET.parse('data.xml')
root = tree.getroot()
emails = [ele.text for ele in list(list(root)[0])]
print(emails)
output输出
['email@email.org', 'email2@email.org', 'email3@email.org']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.