Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting an empty list as attribute when parsing XML with xml.etree.ElementTree

So I use python 3 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 this way.

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

So 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)
{}
like image 843
Kirill Avatar asked Feb 28 '16 14:02

Kirill


1 Answers

.attrib is an empty dictionary in your case since the tags you show don't have any attributes at all. You probably meant to get the .text instead:

doc.find("subjname").text
like image 60
alecxe Avatar answered Nov 14 '22 22:11

alecxe