Not-quite-JSON string deserialization in Python

Posted by cpharmston on Stack Overflow See other posts from Stack Overflow or by cpharmston
Published on 2010-04-04T18:27:56Z Indexed on 2010/04/04 18:33 UTC
Read the original article Hit count: 326

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?

© Stack Overflow or respective owner

Related posts about python

Related posts about deserialization