OrderedDict

silver0346 at gmail.com silver0346 at gmail.com
Wed May 18 04:32:08 EDT 2016


Hi all,

I have a understanding problem with return values from xmltodict.

I have a xml file. Content:

<?xml version="1.0" encoding="utf-8" ?>
<profiles>
  <profile id='visio02' revision='2015051501' >
  <package package-id='0964-gpg4win' />
  </profile>
</profiles>
<!--
-->

With code

__f_name = '<path_to_file>'
with open(__f_name) as __fd:
    __doc = xmltodict.parse(__fd.read())
   
__doc

I get

OrderedDict([(u'profiles', OrderedDict([(u'profile', OrderedDict([(u'@id', u'visio02'), (u'@revision', u'2015051501'), (u'package', OrderedDict([(u'@package-id', u'0964-gpg4win')]))]))]))])

If I use

__doc['profiles']['profile']['package'][0]['@package-id']

I get 

Traceback (most recent call last):
  File "<input>", line 1, in <module>
KeyError: 0

If I change xml file like this:

<?xml version="1.0" encoding="utf-8" ?>
<profiles>
  <profile id='visio02' revision='2015051501' >
  <package package-id='0964-gpg4win' />
  <package package-id='0965-gpg4win' />
  </profile>
</profiles>

and run code from above the result is:

OrderedDict([(u'profiles', OrderedDict([(u'profile', OrderedDict([(u'@id', u'visio02'), (u'@revision', u'2015051501'), (u'package', [OrderedDict([(u'@package-id', u'0964-gpg4win')]), OrderedDict([(u'@package-id', u'0965-gpg4win')])])]))]))])

No prints __doc['profiles']['profile']['package'][0]['@package-id']:

u'0964-gpg4win'

Can everybody explain this?

Many thanks in advance




More information about the Python-list mailing list