Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #83257
| References | <55e354fd-a5bc-4f1f-a59a-55951fe81ca3@googlegroups.com> |
|---|---|
| Date | 2015-01-05 14:45 -0800 |
| Subject | Re: If you were starting a project with XML datasource using python |
| From | Dan Stromberg <drsalists@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.17405.1420497966.18130.python-list@python.org> (permalink) |
On Mon, Jan 5, 2015 at 3:54 AM, flebber <flebber.crue@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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
If you were starting a project with XML datasource using python flebber <flebber.crue@gmail.com> - 2015-01-05 03:54 -0800
Re: If you were starting a project with XML datasource using python Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-01-05 08:56 -0500
Re: If you were starting a project with XML datasource using python flebber <flebber.crue@gmail.com> - 2015-01-05 10:24 -0800
Re: If you were starting a project with XML datasource using python Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-01-05 19:49 -0500
Re: If you were starting a project with XML datasource using python Chris Angelico <rosuav@gmail.com> - 2015-01-06 12:55 +1100
Re: If you were starting a project with XML datasource using python George Silva <georger.silva@gmail.com> - 2015-01-06 00:24 -0200
Re: If you were starting a project with XML datasource using python Dan Stromberg <drsalists@gmail.com> - 2015-01-05 14:45 -0800
Re: If you were starting a project with XML datasource using python Steve Hayes <hayesstw@telkomsa.net> - 2015-01-06 05:59 +0200
csiph-web