2

this is my code

        for code, data in dict_data.items(): 

            try:
                collection2.insert({'_id':code,'data':data})

            except Exception as e:
                print code,'>>>>>>>', str(e)
                sys.exit()

it exited with

         524715 >>>>>>> strings in documents must be valid UTF-8

I could find out the error only by the try catch method. dict_data is a large dictionary which contains calculated values from other collection.

how can i fix this?

thanks

2
  • Are you using Python 2.x or Python 3.x ? Commented Mar 18, 2014 at 9:19
  • I am using python 2.7.5 Commented Mar 18, 2014 at 9:22

1 Answer 1

4

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:

def convert2unicode(mydict):
    for k, v in mydict.iteritems():
        if isinstance(v, str):
            mydict[k] = unicode(v, errors = 'replace')
        elif isinstance(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".

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

4 Comments

Rec, data is a large dictionary in which many strings will be there. How is it possible to provide unicode for all those strings???
I've updated my answer in order to use a helper function to replace all str values in the dict
@ManikandanV, if the answer solved your question, please consider to mark it as "accepted".
@SRC No, the unicode values are replaced in the input param mydict (data)

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.