麻烦解析xml,无法让.find在python ElementTree中工作

时间:2015-11-04 19:15:52

标签: python xml xml-parsing elementtree

我正在尝试从XML文件中提取信息,但我正在努力。

<players root="resources/gfx/characters/costumes/" portraitroot="resources/gfx/ui/boss/" bigportraitroot="resources/gfx/ui/stage/">
    <player id="0" name="Isaac" skin="Character_001_Isaac.png" hp="6" bombs="1"  items="302,105" portrait="PlayerPortrait_01_Isaac.png" bigportrait="PlayerPortraitBig_01_Isaac.png" skinColor="-1" />
    <player id="1" name="Magdalene" skin="Character_002_Magdalene.png" costume="7" hp="8" items="302,45" portrait="PlayerPortrait_02_Magdalene.png" bigportrait="PlayerPortraitBig_02_Magdalene.png" skinColor="-1" />
    <player id="2" name="Cain" skin="Character_003_Cain.png" costume="8" hp="4" keys="1" items="302,46" portrait="PlayerPortrait_03_Cain.png" bigportrait="PlayerPortraitBig_03_Cain.png" skinColor="-1" />
    <player id="3" name="Judas" skin="Character_004_Judas.png" costume="9" hp="2" coins="3" items="302,34" portrait="PlayerPortrait_04_Judas.png" bigportrait="PlayerPortraitBig_04_Judas.png" skinColor="-1" />
    <player id="4" name="???" skin="Character_006_Bluebaby.png" hp="0" armor="6" items="302,36" portrait="PlayerPortrait_06_BlueBaby.png" bigportrait="PlayerPortraitBig_06_Bluebaby.png" skinColor="2" />
    <player id="5" name="Eve" skin="Character_005_Eve.png" costume="10" hp="4" items="302,122,117" portrait="PlayerPortrait_05_Eve.png" bigportrait="PlayerPortraitBig_05_Eve.png" skinColor="-1" />
    <player id="6" name="Samson" skin="Character_007_Samson.png" costume="13" hp="6" items="302,157" portrait="PlayerPortrait_07_Samson.png" bigportrait="PlayerPortraitBig_07_Samson.png" skinColor="-1" />
    <player id="7" name="Azazel" skin="Character_008_Azazel.png" costume="11" hp="0" black="6" card="1" portrait="PlayerPortrait_08_Azazel.png" bigportrait="PlayerPortraitBig_08_Azazel.png" skinColor="1" />
    <player id="8" name="Lazarus" skin="Character_009_Lazarus.png" hp="6" card="40" portrait="PlayerPortrait_09_Lazarus.png" bigportrait="PlayerPortraitBig_09_Lazarus.png" skinColor="-1" />
    <player id="9" name="Eden" skin="Character_009_Eden.png" costume="12" portrait="PlayerPortrait_09_Eden.png" bigportrait="PlayerPortraitBig_09_Eden.png" skinColor="-1">
        <hair gfx="Character_009_EdenHair1.png" />
        <hair gfx="Character_009_EdenHair2.png" />
        <hair gfx="Character_009_EdenHair3.png" />
        <hair gfx="Character_009_EdenHair4.png" />
        <hair gfx="Character_009_EdenHair5.png" />
        <hair gfx="Character_009_EdenHair6.png" />
        <hair gfx="Character_009_EdenHair7.png" />
        <hair gfx="Character_009_EdenHair8.png" />
        <hair gfx="Character_009_EdenHair9.png" />
        <hair gfx="Character_009_EdenHair10.png" />
    </player>
    <player id="10" name="The Lost" skin="Character_012_TheLost.png" hp="0" armor="1" coins="1" portrait="PlayerPortrait_12_TheLost.png" bigportrait="PlayerPortraitBig_12_TheLost.png" skinColor="0" />
    <player id="11" name="Lazarus II" skin="Character_010_Lazarus2.png" hp="2" items="302,214" portrait="PlayerPortrait_10_Lazarus2.png" bigportrait="PlayerPortraitBig_10_Lazarus2.png" skinColor="-1" />
    <player id="12" name="Black Judas" skin="Character_013_BlackJudas.png" black="4" portrait="PlayerPortrait_BlackJudas.png" bigportrait="PlayerPortraitBig_BlackJudas.png" skinColor="1" />
</players>

我正在尝试提取<player>中全部位于根<players>

的项目的数据值
import xml.etree.ElementTree as etree

def mainparse (self,file,param):
    if os.path.exists(file):
        isaac = etree.parse(file)
        players = isaac.findall('player')
        print players
        for c in players:
            player =  c.find('player').text
            print ("{0}".format(player))

    else:
        print ("file doesnt exist")

mainparse("players.xml", "a")

它可以正常工作(这不是提取项目值,但只是<player>中的内容),但它不能处理错误:

player =  c.find('.//player').text
AttributeError: 'NoneType' object has no attribute 'text'

这非常令人困惑,因为它似乎适用于其他所有人。如果我做错了什么是正确的方法?

1 个答案:

答案 0 :(得分:0)

以下是为每个items元素获取player属性值的方法:

from xml.etree import ElementTree as etree

root = etree.parse("players.xml")
players = root.findall('player')
for p in players:
    items = p.get("items")  
    print items

输出:

302,105
302,45
302,46
302,34
302,36
302,122,117
302,157
None
None
None
None
302,214
None