Python dictionary : removing u' chars

Some databases such as Sqlite3 let you define converter and adapter functions so you can retrieve text as str rather than unicode. Unfortunately, MongoDB doesn't provide this option for any of the commonly needed types such as str, decimal or datetime:

  • http://api.mongodb.org/python/current/tutorial.html#a-note-on-unicode-strings
  • http://api.mongodb.org/python/current/faq.html#how-can-i-store-decimal-decimal-instances
  • http://api.mongodb.org/python/current/faq.html#how-can-i-save-a-datetime-date-instance

Having eliminated Mongo options, that leaves writing Python code to do the conversion after the data is retrieved. You could write a recursive function that traverses the result to convert each field.

As a quick-and-dirty alternative, here is a little hack that may be of use:

>>> import json, ast
>>> r = {u'name': u'A', u'primary_key': 1}
>>> ast.literal_eval(json.dumps(r))
{'name': 'A', 'primary_key': 1}

The u characters that you are seeing simply mean that they are unicode strings.

If you do not want them to be unicode, you can encode them as something else, such as ASCII.

>>> s = u'hi!'
>>> s
u'hi'

>>> s2 = s.encode('ascii')
>>> s2
'hi'

If you simply want to convert the dict to json data string you can do:

>>> from bson.json_util import dumps
>>> data = {u'name': u'A', u'primary_key': 1}
>>> dumps(data)
'{"name": "A", "primary_key": 1}'

Tags:

Python

Mongodb