I'd like to convert Unicode into Korean from the response object I brought to the request module.

Asked 2 years ago, Updated 2 years ago, 139 views

import requests

url = 'any_address'
data_payload = {'1', '1'}

res = requests.post(url, data=data_payload)
txt = res.text
print(res.text)

With this source code, res is the object 'requests.models.Response' encoded as utf-8. And if you print out res.text as a test, English comes out well, but Korean comes out as Unicode. For example, the letter "Jung" is printed like \uC815.

In this case, how can I convert Unicode into Korean and print it out?

print(res.text.encode(encoding='utf-8').decode(encoding='utf-8')

I looked it up on the Internet and tried it like this, but it still printed out like Unicode \uC815. I think we should do Unicode encoding before res.text, but I'm not sure.

unicode python string

2022-09-21 11:15

2 Answers

The text of the response object in requests should be a Python Unicode object that has already been decoded by applying encoding. (i.e., no additional encoding or decoding is required.)

The Korean "Jung" is expressed as "\uC815" because the print function is not good at expressing the complex nested Korean alphabet (0xc815 == order('jeong')).)

( https://blog.emacsos.com/unicode-in-python.html )

So, it is very incomplete and dangerous to check the encoding of Korean strings in Python with print statements.

MORE: My answer is not logical. Looking at the questioner's solution, I think the multilingual characters (including Korean) were escaped so that the response text itself was composed only of ASCII.


2022-09-21 11:15

I solved it after doing this and that.

print(json.loads(res.text))

It's printed normally in Korean.


2022-09-21 11:15

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.