Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'url:pypi': 0.03; 'xml,': 0.05; 'json': 0.07; '*args,': 0.09; 'forcing': 0.09; 'subject:using': 0.09; 'url:github': 0.09; 'xml.': 0.09; 'cc:addr :python-list': 0.11; 'def': 0.12; 'jan': 0.12; "'''": 0.16; '**kwargs)': 0.16; '**kwargs):': 0.16; 'attributes.': 0.16; 'cc:name:python list': 0.16; 'choice,': 0.16; 'define:': 0.16; 'singleton': 0.16; 'subject:XML': 0.16; 'traceback.': 0.16; 'url:issues': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; 'subject:project': 0.19; 'import': 0.22; 'cc:addr:python.org': 0.22; 'mon,': 0.24; 'cc:2**0': 0.24; "i've": 0.25; 'header:In- Reply-To:1': 0.27; 'function': 0.29; 'am,': 0.29; 'xml': 0.29; 'returned': 0.30; 'message-id:@mail.gmail.com': 0.30; 'fine,': 0.31; 'file': 0.32; "we're": 0.32; 'worked': 0.33; 'url:python': 0.33; 'skip:d 20': 0.34; 'subject:with': 0.35; 'advice': 0.35; 'except': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'returning': 0.36; 'url:org': 0.36; 'two': 0.37; 'being': 0.38; 'sometimes': 0.38; 'lists.': 0.38; 'needed': 0.38; 'skip:x 10': 0.40; 'times': 0.62; 'choose': 0.64; 'more': 0.64; 'to:addr:gmail.com': 0.65; 'managing': 0.66; '2015': 0.84; 'subject:you': 0.87; 'url:14': 0.95 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:to :cc:content-type; bh=vki0hm9BoMICYmc6KcQ0dBFQOTpX3nbTV/4zR+gNNrI=; b=CoPdlgVsiGUcfh2mFul5it+N9l/OMgtQTUPB3Um/36ONasQB5usROStF+u/WmgwM3J 1KMtVjM7KLjXiM5wpSZEEB2Ed6fGvQWjF+LrTnCMBVEsIZumxnS1ay66YVbAk2mcO7Wi C9H7zqc/aGbW6eThMoxG6g8fzBskFCMZBHUQ0gjKgUi2PXAdS4YV3iLkepEuRlkX/eZK QZkFAUpJbbqBsj4d+J7F/HsQ4xQpns3dAYu+OW1qgz4/HqMB8kOgsD/T12lV+mugJ20j t9VrgaGNUyS98UWv+dOADVlelgBAEUtx7FXI9Y2wg47ifh2wl4F4gpMLAuM/JKsBOSH+ GUAQ== MIME-Version: 1.0 X-Received: by 10.152.170.194 with SMTP id ao2mr97255267lac.12.1420497957687; Mon, 05 Jan 2015 14:45:57 -0800 (PST) In-Reply-To: <55e354fd-a5bc-4f1f-a59a-55951fe81ca3@googlegroups.com> References: <55e354fd-a5bc-4f1f-a59a-55951fe81ca3@googlegroups.com> Date: Mon, 5 Jan 2015 14:45:57 -0800 Subject: Re: If you were starting a project with XML datasource using python From: Dan Stromberg To: flebber Content-Type: text/plain; charset=UTF-8 Cc: Python List X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 29 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1420497966 news.xs4all.nl 2833 [2001:888:2000:d::a6]:51696 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:83257 On Mon, Jan 5, 2015 at 3:54 AM, flebber wrote: > Hi > > I need some advice on managing data in the form of xml. I will have to repeatedly import a small xml file but with many complex attributes. If I have a choice, I choose JSON over XML. If stuck with XML, I like xmltodict: https://pypi.python.org/pypi/xmltodict I've used xmltodict in two projects - both worked well, except I needed to define: def dict_constructor(*args, **kwargs): ''' With this higher order function passed to xmltodict.parse, we're forcing singleton tags to be returned as lists. See also https://github.com/martinblech/xmltodict/issues/14 . ''' return collections.defaultdict(list, *args, **kwargs) ...and use it like: xml_dict = xmltodict.parse(xml_string, dict_constructor=dict_constructor) This was necessary to keep singletons from being returned as singletons, instead returning them as single-element lists. Sometimes singletons are fine, but other times you need to be able to accept 1 or more of something without a traceback. HTH