Python 2.7 JSON dump UnicodeEncodeError Python 2.7 JSON dump UnicodeEncodeError json json

Python 2.7 JSON dump UnicodeEncodeError


The problem you are facing is that the standard file.write() function (called by the json.dump() function) does not support unicode strings. From the error message, it turns out that your string contains the UTF character \U0001f47d (which turns out to code for the character EXTRATERRESTRIAL ALIEN, who knew?), and possibly other UTF characters. To handle these characters, either you can encode them into an ASCII encoding (they'll show up in your output file as \XXXXXX), or you need to use a file writer that can handle unicode.

To do the first option, replace your writing line with this line:

json.dump(unicode(decodedComment), outfile, ensure_ascii = False)

The second option is likely more what you want, and an easy option is to use the codecs module. Import it, and change your second line to:

with codecs.open('_{}'.format(filename), 'w', encoding="utf-8") as outfile:

Then, you'll be able to save the special characters in their original form.