开发者

Not-quite-JSON string deserialization in Python

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?


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).


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.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜