Python: why does str() on some text from a UTF-8 file give a UnicodeDecodeError?

Posted by AP257 on Stack Overflow See other posts from Stack Overflow or by AP257
Published on 2010-03-31T16:10:59Z Indexed on 2010/03/31 16:13 UTC
Read the original article Hit count: 206

Filed under:
|

I'm processing a UTF-8 file in Python, and have used simplejson to load it into a dictionary. However, I'm getting a UnicodeDecodeError when I try to turn one of the dictionary values into a string:

f = open('my_json.json', 'r')
master_dictionary = json.load(f)
#some json wrangling, then it fails on this line...
mysql_string += " ('" + str(v_dict['code'])
Traceback (most recent call last):
  File "my_file.py", line 25, in <module>
    str(v_dict['code']) + "'), "
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf4' in position 35: ordinal not in range(128)

Why is Python even using ASCII? I thought it used UTF-8 by default, and this is a UTF-8 file. What is the problem?

© Stack Overflow or respective owner

Related posts about python

Related posts about character-encoding