Not-quite-JSON string deserialization in Python
- by cpharmston
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?