Answer the question
In order to leave comments, you need to log in
How to set correct encoding for response from server in Python 3?
Hello. I am writing software in Python 3. I need to make a request to the server so that it returns regular JSON. I work with UTF-8 encoding. This encoding is specified everywhere: in the server config, in the header of the PHP script, in the Python script (# -*- coding: utf-8 -*-), the script files themselves are also in UTF-8. But when trying to decode the received response from UTF-8, an error appears:
[Decode error - output not utf-8]
from urllib.request import Request, urlopen
from urllib.parse import urlencode
import json
req = Request("http://devcave.ru/json.php")
response = urlopen(req)
data = response.read().decode('cp1251') # .decode('utf-8') вызывает ошибку, описанную выше
data = json.loads(data)
print(response.headers.get_content_charset())
print(data)
utf-8
{'key': 'Russian language'}
header('Content-Type: application/json;charset=utf-8');
echo json_encode(array('key' => 'русский язык'), JSON_UNESCAPED_UNICODE);
Answer the question
In order to leave comments, you need to log in
In Python 2.7 it works like this :)
>>> data = urlopen('http://devcave.ru/json.php').read()
>>> data.decode('utf-8')
u'{"key":"\\u0440\\u0443\\u0441\\u0441\\u043a\\u0438\\u0439 \\u044f\\u0437\\u044b\\u043a"}'
>>> data.decode('cp1251')
u'{"key":"\\u0440\\u0443\\u0441\\u0441\\u043a\\u0438\\u0439 \\u044f\\u0437\\u044b\\u043a"}'
The encoding of the response from the server must be set on the server.
Campaign the server is crookedly configured. Apache? It is necessary that he does not try to recode everything into this ridiculous cp1251. Well, either edit on the side of the script - do .decode('cp1251') for the data that you receive from the server, if the server is not yours, as you are already doing. This will convert the string to unicode, which you can work with normally.
In short, the server is lying that it gives utf-8. In fact, he gives you cp1251. And Python told you about it.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question