views:

122

answers:

2

I get the following text as a string from an XML-based REST API

'd':4 'ca':5 'sen':1 'diann':2,6,8 'feinstein':3,7,9

that I'm looking to deserialize into a pretty little Python dictionary:

{
    'd': [4],
    'ca': [5],
    'sen': [1],
    'diann': [2, 6, 8],
    'feinstein': [3, 7, 9]
}

I'm hoping to avoid using regular expressions or heavy string manipulation, as this format isn't documented and may change. The best I've been able to come up with:

members = {}
for m in elem.text.split(' '):
    m = m.split(':')
    members[m[0].replace("'", '')] = map(int, m[1].split(','))
    return members

Obviously a terrible approach, but it works, and that's better than anything else I've got right now. Any suggestions on better approaches?

+3  A: 

I would rewrite it as this (the only difference is explicitely naming m[0] and m[1]):

members = {}
for m in elem.text.split(' '):
    key, value = m.split(':')
    members[key.replace("'", '')] = map(int, value.split(','))
    return members

Otherwise this code looks pretty much fine to me. It could be written a little shorter maybe (but that would decrease it's readability).

ChristopheD
One tweak: elem.text.split() is probably better than .split(' '), since it will take multiple spaces as one separator.
Ned Batchelder
Thanks for the help, gentleman! May the nonstandard responses be damned.
cpharmston
+2  A: 

I actually like ChristopheD's answer, but in the interest of exploring other possibilities, there's:

eval("{" + s.replace(":", ":[").replace(" ", "], ") + "]}")

This does some simple replacements to turn the string into legal Python, then uses eval to turn it into a dictionary.

The downsides to this method:

  1. eval is dangerous. If the input isn't trusted (and most isn't), then your system could be compromised.
  2. It's kind of terse and perhaps inflexible if the format changes in a way that's not amenable to converting to Python.
Ned Batchelder
Thanks for the thought! I thought long and hard about the eval possibilities, but this will likely be open-sourced code, so the vulnerability would be known. It's just not worth the risk.
cpharmston
Yes exactly. As I said, I like ChristopheD's answer!
Ned Batchelder