Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #108757
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Peter Otten <__peter__@web.de> |
| Newsgroups | comp.lang.python |
| Subject | Re: OrderedDict |
| Date | Wed, 18 May 2016 11:28:10 +0200 |
| Organization | None |
| Lines | 91 |
| Message-ID | <mailman.2.1463563701.27390.python-list@python.org> (permalink) |
| References | <7522e947-03d5-46e4-a74a-e5b312da47ad@googlegroups.com> <nhhcjb$qe6$1@ger.gmane.org> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Trace | news.uni-berlin.de lqg4N8rpkAXtXfJYxeAv5Qmi/6WGlwXahmhk//y+r14g== |
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; '+++': 0.03; 'diff': 0.05; 'skip:p 60': 0.05; '+class': 0.07; 'everybody,': 0.07; 'content:': 0.09; 'dict': 0.09; 'key)': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'skip:[ 30': 0.09; 'advance': 0.10; 'def': 0.13; '-35,6': 0.16; '__version__': 0.16; 'dictionaries': 0.16; 'key):': 0.16; 'ordereddict': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'wrote:': 0.16; 'library,': 0.18; 'all,': 0.20; '+0200': 0.20; '"",': 0.22; 'keyerror:': 0.22; 'file.': 0.22; 'pass': 0.22; 'this:': 0.23; '(most': 0.24; 'patch': 0.24; 'xml': 0.24; 'header:User-Agent:1': 0.26; 'header:X -Complaints-To:1': 0.26; 'chris': 0.26; 'skip:u 20': 0.28; 'values': 0.28; '---': 0.28; 'prints': 0.29; 'code': 0.30; 'entry': 0.31; 'skip:_ 10': 0.32; 'run': 0.33; 'class': 0.33; 'problem': 0.33; 'source': 0.33; 'skip:_ 30': 0.33; 'traceback': 0.33; 'file': 0.34; 'this?': 0.34; 'skip:p 30': 0.35; 'but': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'thanks': 0.37; 'received:org': 0.37; 'skip:o 20': 0.38; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'skip:u 10': 0.61; 'email addr:gmail.com': 0.62; 'afraid': 0.67; 'everybody': 0.67; 'skip:_ 50': 0.70 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| X-Gmane-NNTP-Posting-Host | p57bd97a1.dip0.t-ipconnect.de |
| User-Agent | KNode/4.13.3 |
| 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 | <nhhcjb$qe6$1@ger.gmane.org> |
| X-Mailman-Original-References | <7522e947-03d5-46e4-a74a-e5b312da47ad@googlegroups.com> |
| Xref | csiph.com comp.lang.python:108757 |
Show key headers only | View raw
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>
> <!--
> -->
>
> 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?
Not everybody, but Chris and a few others can ;)
> Many thanks in advance
I don't see an official way to pass a custom dict type to the library,
but if you are not afraid to change its source code the following patch
will allow you to access the value of dictionaries with a single entry as d[0]:
$ diff -u py2b_xmltodict/local/lib/python2.7/site-packages/xmltodict.py py2_xmltodict/local/lib/python2.7/site-packages/xmltodict.py
--- py2b_xmltodict/local/lib/python2.7/site-packages/xmltodict.py 2016-05-18 11:18:44.000000000 +0200
+++ py2_xmltodict/local/lib/python2.7/site-packages/xmltodict.py 2016-05-18 11:11:13.417665697 +0200
@@ -35,6 +35,13 @@
__version__ = '0.10.1'
__license__ = 'MIT'
+_OrderedDict = OrderedDict
+class OrderedDict(_OrderedDict):
+ def __getitem__(self, key):
+ if key == 0:
+ [result] = self.values()
+ return result
+ return _OrderedDict.__getitem__(self, key)
class ParsingInterrupted(Exception):
pass
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