使用Python从XML中提取信息,输出为lis

2024-05-26 22:56:46 发布

您现在位置: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-&apos;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-&apos;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')

另外,我想得到'任天堂娱乐系统'的价值,以得到回报,以及。你知道吗

在理想情况下,当从另一个函数调用时,该函数将以列表形式返回rom,并以列表形式返回系统名称。你知道吗

谢谢你

  • 初级程序员

Tags: gameenableddescriptionxmlyearcrcratingeurope
2条回答

您需要从XML以迭代方式构建roms列表:

roms = []
for game in games:
    rom_key = game.attributes['name']
    roms.append(rom_key.value)

或者最好写成list-comprehension

roms = [game.attributes['name'].value for game in games]

您还可以使用以下方法提取“任天堂娱乐系统”:

xmldoc.getElementsByTagName('listname')[0].firstChild.data

这给我们留下了:

from xml.dom import minidom

def databaseGameExtraction(xml):
    xmldoc = minidom.parse(xml)
    roms = [game.attributes['name'].value
            for game in xmldoc.getElementsByTagName('game')]
    compagny = xmldoc.getElementsByTagName('listname')[0].childNodes[0].data
    return roms, compagny

roms, compagny = databaseGameExtraction('Nintendo Entertainment System.xml')
print(compagny)
print(roms)

我想你需要

roms = []

for game in games:
    romKey = game.attributes['name']
    roms.append(romKey.value)

print("all roms:", roms)

相关问题 更多 >

    热门问题