OrderedDict

Chris Angelico rosuav at gmail.com
Wed May 18 04:54:04 EDT 2016


On Wed, May 18, 2016 at 6:32 PM,  <silver0346 at gmail.com> wrote:
> 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>
>
> <?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>
>
> No prints __doc['profiles']['profile']['package'][0]['@package-id']:
>
> u'0964-gpg4win'
>
> Can everybody explain this?

This is one of the inherent problems of trying to convert XML (a
structured document format) into a nested dictionary (a structured
tree structure). In a dict, you can't have more than one value
associated with a given key; in XML, and similar container-tag-based
structures, you can - as in this example, where you have two package
tags. When that gets converted into a dictionary, they get joined up
into a list, which is why you can subscript it with the integer 0 to
get the first one. If it's not made into a list, subscriping gives you
the next level of dict straight away.

If you're dealing with some files that have one package and some that
have multiple, the easiest way would be something like this:

packages = __doc['profiles']['profile']['package']
if isinstance(packages, dict):
    packages = [packages]
for package in packages:
    # Do whatever you need with the package
    print(package['@package-id'])

Incidentally, the double-leading-underscore names are not what I'd
recommend; use simple names, and if you need them to be private, put
this into a function (which will make them all function-local by
default). The double leading underscore has special meaning inside a
class, which you most likely don't intend.

ChrisA



More information about the Python-list mailing list