[Python] Getting an empty dictionary as attribute when parsing XML with xml.etree.ElementTree

Discussão em 'Python' iniciado por Stack, Setembro 3, 2025.

  1. Stack

    Stack Membro Participativo

    I use Python 3 and ElementTree to parse an XML.

    text = '''
    <body>
    <list>
    <item>
    <cmid>16934673</cmid>
    <day>29.02.2016</day>
    <relay>1</relay>
    <num>1</num>
    <starttime>08:15</starttime>
    <endtime>08:55</endtime>
    <subjid>81327</subjid>
    <subjname>Литературное чтение</subjname>
    <subjabbr>Лит.чт.</subjabbr>
    <sgid>447683</sgid>
    <sgname>Литературное чтение</sgname>
    <tid>551817</tid>
    <tlastname>Фамилия</tlastname>
    <tfirstname>Имя</tfirstname>
    <tmidname>Отчество</tmidname>
    <roomid>68672</roomid>
    <roomname>Филиал 1 кабинет</roomname>
    </item>
    </list>
    </body>'''


    I try to get subjname, using xml.etree.ElementTree like this:

    >>> import xml.etree.ElementTree as ET
    >>> doc = ET.fromstring(text)
    >>> print(doc[0][0][7].tag)
    subjname
    >>> print(doc[0][0][7].attrib)
    {}


    I always get an empty dict. But I can't find the problem. I thought the problem is that attributes are Cyrillic, but the same problem occurs when I try to get the cmid attribute

    >>> doc = ET.fromstring(r.text.encode('utf-8'))
    >>> print(doc[0][0][0].attrib)
    {}

    Continue reading...

Compartilhe esta Página