7

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)
{}
zx485
  • 28,498
  • 28
  • 50
  • 59
Kirill
  • 163
  • 1
  • 10

1 Answers1

5

.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
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195