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: References: <7522e947-03d5-46e4-a74a-e5b312da47ad@googlegroups.com> 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: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <7522e947-03d5-46e4-a74a-e5b312da47ad@googlegroups.com> Xref: csiph.com comp.lang.python:108757 silver0346@gmail.com wrote: > Hi all, > > I have a understanding problem with return values from xmltodict. > > I have a xml file. Content: > > > > > > > > > > With code > > __f_name = '' > 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 "", line 1, in > KeyError: 0 > > If I change xml file like this: > > > > > > > > > > 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