I'm trying to build a very simple MongoDB extraction script but I stumbled upon this problem of most keynames and strings being recorded as unicode
.
So, when I try to just print a document it comes out with a bunch of keys and values like this u'username':u'christian'
etc.
And my documents in this database are rather large and complex, so I have several nested levels.
I searched around a bit to make this conversion from unicode keys and values to ASCII but came up with nothing.
I'm trying to convert all keys and values like so
def convert2ascii:
for k, v in mydict.iteritems():
newk = k.encode('ascii','ignore')
mydict[newk] = mydict.pop(k)
if isinstance(v, unicode):
mydict[newk] = v.encode('ascii','ignore')
elif isinstance(v, dict):
convert2ascii(v)
#elif isinstance(v, list): // todo
# convert2ascii(v)
But I keep running into some uncovered scenario (like a list of dictionaries, or just a plain list of strings) and having to add all of those to to that function, and by now it is rather ugly.
Any ideas on how I can make that simpler?