0

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?

6
  • Where do you get this kind of string from? Commented Jul 13, 2012 at 16:52
  • eval? Where are you getting this string from? Commented Jul 13, 2012 at 16:52
  • I retrieve it in the body of an API request Commented Jul 13, 2012 at 16:54
  • 1
    looks like response from mongoDB Commented Jul 13, 2012 at 16:56
  • If that's your string literal you could try .replace("u'", "'"), but that's horribly fragile. Commented Jul 13, 2012 at 16:58

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Should explicitly note that using eval() is evil and should be avoided.
1

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

-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.