我想從這個XML文檔中提取數據,並具有輸出是一個列表:使用Python來提取的XML信息,輸出作爲一個列表
例如:
['10-Yard Fight (USA, Europe)', '1942 (Japan, USA)', .......]
我可以只弄清楚如何製作許多獨立的名單。
例如:
['10-Yard Fight (USA, Europe)']
['1942 (Japan, USA)']
[.......]
XML示例:
<?xml version="1.0"?>
<menu>
<header>
<listname>Nintendo Entertainment System</listname>
<id>003</id>
<lastlistupdate>10/16/2014</lastlistupdate>
<listversion>1.1 Final</listversion>
<manufacturer>Nintendo</manufacturer>
<media>
<artwork></artwork>
<video></video>
</media>
<exporterversion>HyperList XML Exporter Version 1.3 Copywrite (c) 2009-2011 William Strong</exporterversion>
</header>
<game name="10-Yard Fight (USA, Europe)" index="true" image="1" id="0034232">
<description>10-Yard Fight (USA, Europe)</description>
<cloneof></cloneof>
<crc>3D564757</crc>
<manufacturer>Nintendo</manufacturer>
<year>1985</year>
<genre>Football/Sports</genre>
<rating>HSRS - GA (General Audience)</rating>
<enabled>Yes</enabled>
</game>
<game name="1942 (Japan, USA)" index="" image="">
<description>1942 (Japan, USA)</description>
<cloneof></cloneof>
<crc>171251E3</crc>
<manufacturer>Capcom</manufacturer>
<year>1986</year>
<genre>Shoot-'Em-Up</genre>
<rating>HSRS - GA (General Audience)</rating>
<enabled>Yes</enabled>
</game>
<game name="1943 - The Battle of Midway (USA)" index="" image="">
<description>1943 - The Battle of Midway (USA)</description>
<cloneof></cloneof>
<crc>12C6D5C7</crc>
<manufacturer>Capcom</manufacturer>
<year>1988</year>
<genre>Shoot-'Em-Up</genre>
<rating>HSRS - GA (General Audience)</rating>
<enabled>Yes</enabled>
</game>
</menu>
我的樣品Python代碼
from xml.dom import minidom
def databaseGameExtraction(xml):
xmldoc = minidom.parse(xml)
games = xmldoc.getElementsByTagName('game')
for game in games:
romKey = game.attributes['name']
roms = [romKey.value]
print(roms)
return roms
databaseGameExtraction('Nintendo Entertainment System.xml')
還,我希望得到的 'Nintendo娛樂系統' 的值是也返回。
在完美的世界中,當從另一個函數調用時,函數將返回列表形式的rom和列表形式的系統名稱。
感謝,
- 一個很初級編碼器
這工作太感謝 –