Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #108756
| Path | csiph.com!feeder.erje.net!2.eu.feeder.erje.net!newsfeed.kamp.net!newsfeed.kamp.net!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: OrderedDict |
| Date | Wed, 18 May 2016 18:54:04 +1000 |
| Lines | 55 |
| Message-ID | <mailman.1.1463561645.27390.python-list@python.org> (permalink) |
| References | <7522e947-03d5-46e4-a74a-e5b312da47ad@googlegroups.com> <CAPTjJmomMDzVkYbeT9z4R-npQJPe-XnoLk+sGmh6W8ua65qqHQ@mail.gmail.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| X-Trace | news.uni-berlin.de bOByev2zfQzxQnbeqK7ubQDqb/l+t7q9brr1HDguhSVA== |
| Return-Path | <rosuav@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.007 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'xml,': 0.05; 'cc:addr :python-list': 0.09; 'content:': 0.09; 'dict': 0.09; 'underscore': 0.09; 'wed,': 0.15; '2016': 0.16; 'default).': 0.16; 'dictionary,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'multiple,': 0.16; 'packages:': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'tags.': 0.16; 'wrote:': 0.16; 'integer': 0.18; 'nested': 0.18; 'tree': 0.18; 'all,': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'class,': 0.22; 'converted': 0.22; 'file.': 0.22; 'trying': 0.22; 'this:': 0.23; 'xml': 0.24; 'header:In-Reply-To:1': 0.24; '(which': 0.26; 'message-id:@mail.gmail.com': 0.27; 'function': 0.28; 'values': 0.28; 'away.': 0.29; 'dictionary': 0.29; 'prints': 0.29; 'convert': 0.29; "i'd": 0.31; "can't": 0.32; 'problem': 0.33; 'skip:_ 30': 0.33; 'joined': 0.33; 'similar': 0.33; 'this?': 0.34; 'skip:d 20': 0.34; 'gets': 0.35; 'gives': 0.35; 'received:google.com': 0.35; 'next': 0.35; 'easiest': 0.35; 'something': 0.35; 'level': 0.35; 'received:74.125.82': 0.35; 'list,': 0.36; 'skip:i 20': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'associated': 0.38; 'names': 0.38; 'skip:p 20': 0.38; 'files': 0.38; 'why': 0.39; 'whatever': 0.39; 'where': 0.40; 'some': 0.40; 'leading': 0.61; 'skip:u 10': 0.61; 'more': 0.63; 'subscript': 0.66; 'everybody': 0.67; 'skip:_ 50': 0.70; 'special': 0.73; 'chrisa': 0.84; 'dict,': 0.84; 'inherent': 0.84; 'to:none': 0.91 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc; bh=l+sT00iJAHTT6sR1WCgetZ8m34KFBcGKn/WkEWspmLQ=; b=1GTAIMLk7MlfZBFSrqGDKlzrLbWT4Inng7hxPkSJMEt2QRGTyh1KvKq6YTyub0Ws7U /nFg8mfakyRE6q1vbASaWIBDkeMD5ZqDpBRpC5Dkx2AhF3XpYqof5fL6ec7r9y2MB7wU 0FUd/SL+hkH26SFu5YqgHz49BKtUqE4zO1q6mMTpBWJ/0VS4oQJl4/eRx2PECrEYtwPZ BMgW5QzGDE9a5HBlYyadNNUX2sSnhlSJtlsI/9Mal6GIN0KG0rKljSXU75ISjGk+NZNw LwLTWRCQjNwBezB9dG2QWfD31y/DnQpuNkrel8BqrlVGeYRJZ3TKE3tRHKeEE2uu1pnk rmgw== |
| X-Google-DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc; bh=l+sT00iJAHTT6sR1WCgetZ8m34KFBcGKn/WkEWspmLQ=; b=LPu4+Xk7B4CPBnXSjLB7z2zfH5tXvsvlevPJLS0VcgtzFQ1KdyeCQNEyeOJ9Qhqjiz 5bBrVioIhe+orPBnKCaL6aGlkpo/on0y7woVbZwvTbek9zO8B1Ms1Dz5QWhCSbvUCrXH HCdyoZ9n9gDYCuQWmK+P2qzVzq+x5gMgI6sGlsn5zXZwHqP0j7Lnt3RGQ3eE4ls4arFn N2rHVJkPtpznWrulitbAPnZEXCFNy5MojlR8B8rMOic++bA0ul6bxqQrXWMYQGVNqcY5 0ITs2O1rZ6VLcVbwZttx1lXAMKOcZfFRoL6kdJMKagGBGyOR4KFC7bKQorYVBpI0gnFl aDGg== |
| X-Gm-Message-State | AOPr4FWqWjok6DSD63Pd3Ncuf+WOwjADpcF5m7bJWg7rjEgAd2SJex8v5BYX7c6vd6PC5CaIhMW8VNWlY8nk4w== |
| X-Received | by 10.194.86.129 with SMTP id p1mr5818106wjz.117.1463561644156; Wed, 18 May 2016 01:54:04 -0700 (PDT) |
| In-Reply-To | <7522e947-03d5-46e4-a74a-e5b312da47ad@googlegroups.com> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.22 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| X-Mailman-Original-Message-ID | <CAPTjJmomMDzVkYbeT9z4R-npQJPe-XnoLk+sGmh6W8ua65qqHQ@mail.gmail.com> |
| X-Mailman-Original-References | <7522e947-03d5-46e4-a74a-e5b312da47ad@googlegroups.com> |
| Xref | csiph.com comp.lang.python:108756 |
Show key headers only | View raw
On Wed, May 18, 2016 at 6:32 PM, <silver0346@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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
OrderedDict silver0346@gmail.com - 2016-05-18 01:32 -0700
Re: OrderedDict Chris Angelico <rosuav@gmail.com> - 2016-05-18 18:54 +1000
Re: OrderedDict Peter Otten <__peter__@web.de> - 2016-05-18 11:28 +0200
Re: OrderedDict Chris Angelico <rosuav@gmail.com> - 2016-05-18 20:47 +1000
Re: OrderedDict Peter Otten <__peter__@web.de> - 2016-05-18 14:24 +0200
Re: OrderedDict silver0346@gmail.com - 2016-05-19 22:15 -0700
Re: OrderedDict silver0346@gmail.com - 2016-06-09 03:02 -0700
csiph-web