A
A
Alexander2020-01-20 14:40:03
Python
Alexander, 2020-01-20 14:40:03

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'])

there are Russian words in the answer, but they are encoded
{"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":""}]}

Actually the question is how to get "Obtained" from "\u041e\u0434\u0435\u0440\u0436\u0430\u043d\u043e"? Because even these characters the python does not want to display, it gives an error
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-7: ordinal not in range(128)

I tried to decode and encode for everyone, nothing helps ((

Answer the question

In order to leave comments, you need to log in

4 answer(s)
B
bbkmzzzz, 2020-01-20
@sajini

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

A
Andrey, 2020-01-20
@anerev

b"\u041e\u0434\u0435\u0440\u0436\u0430\u043d\u043e".decode('unicode-escape')

P
PaulTinnik, 2020-11-01
@PaulTinnik

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>

M
MAD_CHE, 2022-04-16
@MAD_CHE

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 question

Ask a Question

731 491 924 answers to any question