Answer the question
In order to leave comments, you need to log in
How to get Russian text from json in Python?
Trying to parse json response in python
import requests
import json
apiUrl = "http://openapi.justin.ua/tracking/"
track = '201810165'
response = requests.get(apiUrl + track, verify=False)
obj = json.loads(response.text)
print(obj['result'][-1]['status'])
{"status":1,"msg":null,"result":[{"orderNumber":"201810165","orderDescription":"\u0417\u0430\u043c\u043e\u0432\u043b\u0435\u043d\u043d\u044f \u043a\u043b\u0456\u0454\u043d\u0442\u0430 201810165 \u0432\u0456\u0434 7\/25\/2018","date":"2019-02-27","time":"10:20:51","status":"\u041e\u0434\u0435\u0440\u0436\u0430\u043d\u043e","departmentNumber":"","departmentAdress":""}]}
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-7: ordinal not in range(128)
Answer the question
In order to leave comments, you need to log in
So problems with writing to a file?
json.dump(obj, fp, *, skipkeys=False, ensure_ascii=True , check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw)
set to False, json encodes unicode by default like this. The file must be opened in utf-8
b"\u041e\u0434\u0435\u0440\u0436\u0430\u043d\u043e".decode('unicode-escape')
Perhaps it means the encoding, directly when receiving json from the server.
Then the solution is this. We check the current encoding in the Requests library.
print(responce.encoding) # ISO-8859-1
Меняем на utf-8
<code lang="python">
responce.encoding = "utf-8"
</code>
Вместе приблизительно так:
<code lang="python">
url = "https://your_url.me"
responce = requests.get(url=url)
responce.encoding = "utf-8" # После этой строки кириллические символы вместо экранированных
</code>
You check the languages with the command:
locale
In place of LC_CTYPE there should be a UTF-8 mark, if not, then write:
export LC_CTYPE="en_US.UTF-8"
And if it doesn’t help, then change the encoding for all values:
export LC_ALL="en_US.UTF-8"
I immediately registered for everyone and began to receive information in human language.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question