3 回答

TA貢獻1808條經驗 獲得超4個贊
首先,您可以找到行星和恒星元素,將它們添加到列表中,然后從中選擇第一個值。
import xml.etree.ElementTree as ET
dump = ET.parse('planet_info.xml')
root = dump.getroot()
planet_name = star_name = planet_list = []
for content in root:
planet_name = content.findall("planet")
star_name = content.findall("star")
if planet_name:
for planet in planet_name:
planet_list.append(Planet(planet[0].text, planet[1].text, etc)
elif star_name:
for star in star_name:
planet_list.append(Planet(star[0].text, star[1].text, etc)
else:
print("Nothing")

TA貢獻1821條經驗 獲得超5個贊
此代碼使用etree起作用。您缺少一行僅返回item已name設置為某個值的元素的行。
import xml.etree.ElementTree as ET
root = ET.parse(PATH_TO_YOUR_FILE)
planets = root.findall('./planets/planet')
planets_list = []
for planet in planets:
name = planet.find("./item[@name='Name']").text
d = planet.find("./item[@name='Distance from the Earth']").text
//...
planets_list.append(Planet(name, d, ...)
從這里您應該可以自己解決其余的問題。希望能幫助到你。

TA貢獻1824條經驗 獲得超6個贊
這是一種遍歷該數據結構的簡單方法。為了使代碼保持獨立,我直接從字符串而不是從文件讀取XML。
import xml.etree.ElementTree as ET
data = '''\
<?xml version="1.0"?>
<celestialBodies>
<stars>
<star>
<item name="Name">Sun</item>
<item name="Distance from the Earth">100000</item>
<item name="Size">9</item>
<item name="Moons">A,B,C</item>
<item name="Gravity">0.4</item>
<item name="Elements Found">H, HE</item>
<item name="Orbiting Time">15</item>
<item name="Day Time">12</item>
</star>
</stars>
<planets>
<planet>
<item name="Name">Mercury</item>
<item name="Distance from the Earth">100000</item>
<item name="Size">9</item>
<item name="Moons">A,B,C</item>
<item name="Gravity">0.4</item>
<item name="Elements Found">H, HE, C</item>
<item name="Orbiting Time">15</item>
<item name="Day Time">12</item>
</planet>
<planet>
<item name="Name">Venus</item>
<item name="Distance from the Earth">100000</item>
<item name="Size">9</item>
<item name="Moons">A,B,C</item>
<item name="Gravity">0.4</item>
<item name="Elements Found">H, HE, C</item>
<item name="Orbiting Time">15</item>
<item name="Day Time">12</item>
</planet>
<planet>
<item name="Name">Earth</item>
<item name="Distance from the Earth">0</item>
<item name="Size">9</item>
<item name="Moons">A,B,C</item>
<item name="Gravity">0.4</item>
<item name="Elements Found">H, HE, C</item>
<item name="Orbiting Time">15</item>
<item name="Day Time">12</item>
</planet>
<planet>
<item name="Name">Mars</item>
<item name="Distance from the Earth">100000</item>
<item name="Size">9</item>
<item name="Moons">A,B,C</item>
<item name="Gravity">0.4</item>
<item name="Elements Found">H, HE, C</item>
<item name="Orbiting Time">15</item>
<item name="Day Time">12</item>
</planet>
<planet>
<item name="Name">Jupiter</item>
<item name="Distance from the Earth">100000</item>
<item name="Size">9</item>
<item name="Moons">A,B,C</item>
<item name="Gravity">0.4</item>
<item name="Elements Found">H, HE, C</item>
<item name="Orbiting Time">15</item>
<item name="Day Time">12</item>
</planet>
<planet>
<item name="Name">Saturn</item>
<item name="Distance from the Earth">100000</item>
<item name="Size">9</item>
<item name="Moons">A,B,C</item>
<item name="Gravity">0.4</item>
<item name="Elements Found">H, HE, C</item>
<item name="Orbiting Time">15</item>
<item name="Day Time">12</item>
</planet>
<planet>
<item name="Name">Uranus</item>
<item name="Distance from the Earth">100000</item>
<item name="Size">9</item>
<item name="Moons">A,B,C</item>
<item name="Gravity">0.4</item>
<item name="Elements Found">H, HE, C</item>
<item name="Orbiting Time">15</item>
<item name="Day Time">12</item>
</planet>
<planet>
<item name="Name">Neptune</item>
<item name="Distance from the Earth">100000</item>
<item name="Size">9</item>
<item name="Moons">A,B,C</item>
<item name="Gravity">0.4</item>
<item name="Elements Found">H, HE, C</item>
<item name="Orbiting Time">15</item>
<item name="Day Time">12</item>
</planet>
<planet>
<item name="Name">Pluto</item>
<item name="Distance from the Earth">100000</item>
<item name="Size">9</item>
<item name="Moons">A,B,C</item>
<item name="Gravity">0.4</item>
<item name="Elements Found">H, HE, C</item>
<item name="Orbiting Time">15</item>
<item name="Day Time">12</item>
</planet>
</planets>
</celestialBodies>
'''
root = ET.fromstring(data)
for kind in root:
print(kind.tag)
for child in kind:
print(' '*2, child.tag)
for item in child:
print(' '*4, '{!r}: {!r}'.format(item.attrib['name'], item.text))
print()
輸出
stars
star
'Name': 'Sun'
'Distance from the Earth': '100000'
'Size': '9'
'Moons': 'A,B,C'
'Gravity': '0.4'
'Elements Found': 'H, HE'
'Orbiting Time': '15'
'Day Time': '12'
planets
planet
'Name': 'Mercury'
'Distance from the Earth': '100000'
'Size': '9'
'Moons': 'A,B,C'
'Gravity': '0.4'
'Elements Found': 'H, HE, C'
'Orbiting Time': '15'
'Day Time': '12'
planet
'Name': 'Venus'
'Distance from the Earth': '100000'
'Size': '9'
'Moons': 'A,B,C'
'Gravity': '0.4'
'Elements Found': 'H, HE, C'
'Orbiting Time': '15'
'Day Time': '12'
planet
'Name': 'Earth'
'Distance from the Earth': '0'
'Size': '9'
'Moons': 'A,B,C'
'Gravity': '0.4'
'Elements Found': 'H, HE, C'
'Orbiting Time': '15'
'Day Time': '12'
planet
'Name': 'Mars'
'Distance from the Earth': '100000'
'Size': '9'
'Moons': 'A,B,C'
'Gravity': '0.4'
'Elements Found': 'H, HE, C'
'Orbiting Time': '15'
'Day Time': '12'
planet
'Name': 'Jupiter'
'Distance from the Earth': '100000'
'Size': '9'
'Moons': 'A,B,C'
'Gravity': '0.4'
'Elements Found': 'H, HE, C'
'Orbiting Time': '15'
'Day Time': '12'
planet
'Name': 'Saturn'
'Distance from the Earth': '100000'
'Size': '9'
'Moons': 'A,B,C'
'Gravity': '0.4'
'Elements Found': 'H, HE, C'
'Orbiting Time': '15'
'Day Time': '12'
planet
'Name': 'Uranus'
'Distance from the Earth': '100000'
'Size': '9'
'Moons': 'A,B,C'
'Gravity': '0.4'
'Elements Found': 'H, HE, C'
'Orbiting Time': '15'
'Day Time': '12'
planet
'Name': 'Neptune'
'Distance from the Earth': '100000'
'Size': '9'
'Moons': 'A,B,C'
'Gravity': '0.4'
'Elements Found': 'H, HE, C'
'Orbiting Time': '15'
'Day Time': '12'
planet
'Name': 'Pluto'
'Distance from the Earth': '100000'
'Size': '9'
'Moons': 'A,B,C'
'Gravity': '0.4'
'Elements Found': 'H, HE, C'
'Orbiting Time': '15'
'Day Time': '12'
這種變化將數據加載到包含兩個列表的字典中,一個列表用于恒星,一個列表用于行星。這些列表中的每個列表都包含該類別中每個正文的字典。我們可以使用該json模塊很好地打印數據。
import xml.etree.ElementTree as ET
import json
root = ET.fromstring(data)
bodies = {}
for kind in root:
bodies[kind.tag] = bodylist = []
for child in kind:
bodylist.append({item.attrib['name']: item.text for item in child})
print(json.dumps(bodies, indent=4))
輸出
{
"stars": [
{
"Name": "Sun",
"Distance from the Earth": "100000",
"Size": "9",
"Moons": "A,B,C",
"Gravity": "0.4",
"Elements Found": "H, HE",
"Orbiting Time": "15",
"Day Time": "12"
}
],
"planets": [
{
"Name": "Mercury",
"Distance from the Earth": "100000",
"Size": "9",
"Moons": "A,B,C",
"Gravity": "0.4",
"Elements Found": "H, HE, C",
"Orbiting Time": "15",
"Day Time": "12"
},
{
"Name": "Venus",
"Distance from the Earth": "100000",
"Size": "9",
"Moons": "A,B,C",
"Gravity": "0.4",
"Elements Found": "H, HE, C",
"Orbiting Time": "15",
"Day Time": "12"
},
{
"Name": "Earth",
"Distance from the Earth": "0",
"Size": "9",
"Moons": "A,B,C",
"Gravity": "0.4",
"Elements Found": "H, HE, C",
"Orbiting Time": "15",
"Day Time": "12"
},
{
"Name": "Mars",
"Distance from the Earth": "100000",
"Size": "9",
"Moons": "A,B,C",
"Gravity": "0.4",
"Elements Found": "H, HE, C",
"Orbiting Time": "15",
"Day Time": "12"
},
{
"Name": "Jupiter",
"Distance from the Earth": "100000",
"Size": "9",
"Moons": "A,B,C",
"Gravity": "0.4",
"Elements Found": "H, HE, C",
"Orbiting Time": "15",
"Day Time": "12"
},
{
"Name": "Saturn",
"Distance from the Earth": "100000",
"Size": "9",
"Moons": "A,B,C",
"Gravity": "0.4",
"Elements Found": "H, HE, C",
"Orbiting Time": "15",
"Day Time": "12"
},
{
"Name": "Uranus",
"Distance from the Earth": "100000",
"Size": "9",
"Moons": "A,B,C",
"Gravity": "0.4",
"Elements Found": "H, HE, C",
"Orbiting Time": "15",
"Day Time": "12"
},
{
"Name": "Neptune",
"Distance from the Earth": "100000",
"Size": "9",
"Moons": "A,B,C",
"Gravity": "0.4",
"Elements Found": "H, HE, C",
"Orbiting Time": "15",
"Day Time": "12"
},
{
"Name": "Pluto",
"Distance from the Earth": "100000",
"Size": "9",
"Moons": "A,B,C",
"Gravity": "0.4",
"Elements Found": "H, HE, C",
"Orbiting Time": "15",
"Day Time": "12"
}
]
}
添加回答
舉報