S
S
saalaus2019-05-27 21:01:49
Python
saalaus, 2019-05-27 21:01:49

Encoding issue when requesting VK api in Python?

I make a request for the messages.getConversations method, and an encoding error appears in the console

param = dict(access_token = 'mytoken',
            version = 5.95,
            count = 10
            )

chat = requests.get('https://api.vk.com/method/messages.getConversations', param)

print(chat.text)

mistake
Traceback (most recent call last):
File "C:\Users\User\Desktop\main.py", line 22, in <module>
print(chat.text)
File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\encodings\cp1251.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\U0001f4cc' in position 6739: character maps to <undefined>

added , but now I can't convert it to a JSON object, and characters like "\U0001f4cc" are left instead of Russian letters. How can I make Russian letters appear and get a JSON object?
print(chat.text.encode("utf-8"))

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vyacheslav, 2019-05-28
@saalaus

encode()must be used together with decode(), then the console will print everything that is needed:
"\U0001f4cc".encode(encoding='utf-8').decode()

B
bbkmzzzz, 2019-05-28
@bbkmzzzz

What encoding does the console use for output?
When dumping to json , set ensure_ascii=False , but note that not all text editors will be able to display some characters correctly

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question