I want to convert a string which is like that:
{u'confirm_token': u'98c21e111f25550943e29e34e65ae1dd71968ff652cb933c2f998e4f',
u'confirmed_account': False, u'confidential': {}, u'contacts': [],
u'_id': ObjectId('4e0a761d7c93dd25bc000021'), u'settings': {u'email': u''},
u'here': [], u'creation_date': datetime.datetime(2011, 6, 29, 2, 47, 25),
u'profil': {u'gender': u'Male', u'first_name': u'Test', u'last_name': u'Test',
u'email_address': u'[email protected]', u'photo': u'picture.png'},
u'attending': [], u'requests': [],
u'password': u'b04b55d5f4555e5d7252e7f74aaf4dc538639fa6864f3d8004c61635'}
to a dictionary in Python. I actually tried to use the function json.load() but it doesn't work because of the Unicode before each keys and values. Does anyone know how to do it?
3 Answers 3
The only easy way to transform this into a Python object is to use eval() (provided ObjectId is some valid Python class). This will only be an option if the string is from a trusted source. The more secure function ast.literal_eval() won't work for this case – it can't evaluate the ObjectId() call.
You should really try to fix whatever you get this string from to use a sane serialisation format.
1 Comment
ast.literal_eval() (or rather its implementation) will get you most of the way there. The rest will be walking the AST to replace the ObjectId() call appropriately.
Comments
Why not split up this string and then store in dictionary. I had the same problem and neither of eval or literal_eval worked for me
eval? Where are you getting this string from?.replace("u'", "'"), but that's horribly fragile.