Skip to content Skip to sidebar Skip to footer

Mongodb Insertion Shows 'strings In Documents Must Be Valid Utf-8'

this is my code for code, data in dict_data.items(): try: collection2.insert({'_id':code,'data':data}) except Exception as e:

Solution 1:

If you are using PyMongo and Python 2.x, you should use str in utf-8 or unicode strings. See: http://api.mongodb.org/python/current/tutorial.html#a-note-on-unicode-strings

If datais a dict with multiple strings you can convert all of them to unicode using following function:

defconvert2unicode(mydict):
    for k, v in mydict.iteritems():
        ifisinstance(v, str):
            mydict[k] = unicode(v, errors = 'replace')
        elifisinstance(v, dict):
            convert2unicode(v)

for code, data in dict_data.items(): 
    try:
        convert2unicode(data)
        collection2.insert({'_id':code,'data': data})
    except Exception as e:
        print code,'>>>>>>>', str(e)
        sys.exit()

Previous code will convert all str values in unicode, the "keys" keep untouched, depending on root cause you should also convert the "keys".

Post a Comment for "Mongodb Insertion Shows 'strings In Documents Must Be Valid Utf-8'"