If you were starting a project with XML datasource using python

Dan Stromberg drsalists at gmail.com
Mon Jan 5 17:45:57 EST 2015


On Mon, Jan 5, 2015 at 3:54 AM, flebber <flebber.crue at gmail.com> 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



More information about the Python-list mailing list