4
Данный входной файл с содержанием:получение UnicodeEncodeError при попытке записи JSON файлу
{ "symbol": "°C" }
И этот код:
import sys
import json
with open(sys.argv[1], 'r') as ifile, open(sys.argv[2], 'w') as ofile:
json.dump(json.load(ifile), ofile, indent=4, ensure_ascii=False)
Я получаю сообщение об ошибке:
$ python2.7 play.py input.json output.json
Traceback (most recent call last):
File "play.py", line 5, in <module>
json.dump(json.load(ifile), ofile, indent=4, ensure_ascii=False)
File "/usr/lib/python2.7/json/__init__.py", line 190, in dump
fp.write(chunk)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xb0' in position 1: ordinal not in range(128)
Но Python 3 отлично работает:
$ python3.3 play.py input.json output.json
$ cat output.json
{
"symbol": "°C"
}
возможный дубликат [UnicodeEncodeError при записи в файл] (http://stackoverflow.com/questions/6939692/unicodeencodeerror-когда-письма к-а-файл) – Tadeck