homepage

This issue tracker has been migrated to GitHub , and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Reading unicode json string fails depending on LANG env
Type: Stage:
Components: Unicode Versions: Python 3.4
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: ezio.melotti, martin.panter, vstinner, yves
Priority: normal Keywords:

Created on 2015年09月08日 09:46 by yves, last changed 2022年04月11日 14:58 by admin. This issue is now closed.

Messages (2)
msg250175 - (view) Author: Yves Müller (yves) Date: 2015年09月08日 09:46
I am trying to read json containing a UTF-8 string from a file. It works when running it from a shell with LANG=en_GB.utf8 set, but fails from the empty environment.
> python3 --version
Python 3.4.0
> cat test.json 
{ "test": "Ümläute" }
> env -u LANG python3 -c 'import json; json.load(open("test.json"))'
Traceback (most recent call last):
 File "<string>", line 1, in <module>
 File "/usr/lib/python3.4/json/__init__.py", line 265, in load
 return loads(fp.read(),
 File "/usr/lib/python3.4/encodings/ascii.py", line 26, in decode
 return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 11: ordinal not in range(128)
> > env LANG=en_GB.UTF-8 python3 -c 'import json; print(json.load(open("test.json")))'
{'test': 'Ümläute'}
msg250180 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2015年09月08日 11:07
By default, open("test.json") will open the file in text mode, decoding from the encoding determined by your environment. See <https://docs.python.org/3.4/library/functions.html#open>, in particular, "The default encoding is platform dependent (whatever locale.getpreferredencoding() returns)".
If your file’s encoding is determined some other way, you should specify that other encoding. For JSON, I understand UTF-8 is generally always used, so you should call open("test.json", encoding="utf-8").
History
Date User Action Args
2022年04月11日 14:58:20adminsetgithub: 69216
2015年09月08日 11:08:00martin.pantersetstatus: open -> closed

nosy: + martin.panter
messages: + msg250180

resolution: not a bug
2015年09月08日 09:46:02yvescreate

AltStyle によって変換されたページ (->オリジナル) /