[英]How can I extract all content from all elements of an XML file with Python and ElementTree?
I have the following XML file called Artists.xml, which contains information on several artists as shown bellow: 我有以下名为Artists.xml的XML文件,其中包含几个艺术家的信息,如下所示:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Artists>
<Singer name="Britney">
<Albums>7</Albums>
<Country>USA</County>
<Last Single> Piece of Me
<Year>2011</Year>
</Last Single>
</Singer>
<Singer name="Justin">
<Albums>8</Albums>
<Country>USA</County>
<Last Single> Rock Your Body
<Year>2004</Year>
</Last Single>
</Singer>
</Artsts>
I am using the Python library ElementTree in order to extract the content of all tags. 我正在使用Python库ElementTree来提取所有标签的内容。 So far this is the Python code I have written:
到目前为止,这是我编写的Python代码:
from xml.etree import cElementTree as ET
tree = ET.parse('Artists.xml')
root = tree.getroot()
for child in root:
for content in child:
print(child[content].text)
Despite that, when I run the script I see no inputs in my console. 尽管如此,当我运行脚本时,我在控制台中看不到任何输入。 I would like to see something like :
7 USA Piece of Me 2011, 8 USA Rock Your Body 2004.
Could someone help me understand what I am doing wrong? 我希望看到类似的东西:
7 USA Piece of Me 2011, 8 USA Rock Your Body 2004.
有人能帮我理解我做错了吗? Thanks in advance! 提前致谢!
using xml.etree.ElementTree
使用
xml.etree.ElementTree
test.xml: 的test.xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Artists>
<Singer name="Britney">
<Albums>7</Albums>
<Country>USA</Country>
<LastSingle>
Piece of Me
<Year>2011</Year>
</LastSingle>
</Singer>
<Singer name="Justin">
<Albums>8</Albums>
<Country>USA</Country>
<LastSingle> Rock Your Body
<Year>2004</Year>
</LastSingle>
</Singer>
</Artists>
Hence : 因此 :
from xml.etree import ElementTree
tree = ElementTree.parse('test.xml')
root = tree.getroot()
results = root.findall('Singer')
for elem in results:
for e in elem:
print(e.text.strip())
OUTPUT : 输出 :
7
USA
Piece of Me
8
USA
Rock Your Body
Process finished with exit code 0
A generic approach. 一般方法。 Convert the XML to dict and print the dict.
将XML转换为dict并打印dict。 (The file 55726013.xml contains your sample data).
(文件55726013.xml包含您的示例数据)。 As you can see the code has zero knowledge about the XML structure.
如您所见,代码对XML结构没有任何了解。
import xmltodict
import json
with open('55726013.xml') as fd:
doc = xmltodict.parse(fd.read())
print(json.dumps(doc, indent=4))
Output 产量
{
"Artists": {
"Singer": [
{
"@name": "Britney",
"Albums": "7",
"Country": "USA",
"LastSingle": {
"Year": "2011",
"#text": "Piece of Me"
}
},
{
"@name": "Justin",
"Albums": "8",
"Country": "USA",
"LastSingle": {
"Year": "2004",
"#text": "Rock Your Body"
}
}
]
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.