Converting raw unicode to text

Viewed 100

I am coding with python 3.8 to get some text from html sources with selenium.

I got something like this from page_source (the html_source is too long, so I just show the focus part for output of print()):

html_source = browser.page_source
print(html_source)
>>> \u514d\u8cbb\u8cc7\u6e90\u7db2\u8def\u793e\u7fa4

But my expected output is:

print(html_source)
>>> 免費資源網路社群

I tried to do this:

html_source.encode(‘utf-8’)
# but the result is same
print(html_source)
>>> \u514d\u8cbb\u8cc7\u6e90\u7db2\u8def\u793e\u7fa4

Seems it is because the Unicode in html_source is raw string. So how can I convert the raw Unicode to my expected output?

I am new to coding, and get mess about text encode decode. I will be grateful for any help you can provide.

Supplement:

# I discover that r’’ string will give a similar result as above
html_source = r”\u514d\u8cbb\u8cc7\u6e90\u7db2\u8def\u793e\u7fa4”
print(html_source)
>>> \u514d\u8cbb\u8cc7\u6e90\u7db2\u8def\u793e\u7fa4

# while if the string is normal
html_source = “\u514d\u8cbb\u8cc7\u6e90\u7db2\u8def\u793e\u7fa4”
print(html_source)
>>> 免費資源網絡社群
2 Answers

Python 3:

s = r'\u514d\u8cbb\u8cc7\u6e90\u7db2\u8def\u793e\u7fa4'
print(s.encode('utf-8').decode('unicode-escape'))

Python 2

s = r'\u514d\u8cbb\u8cc7\u6e90\u7db2\u8def\u793e\u7fa4'
print (s.decode('unicode-escape').encode('utf-8'))

Try using

utf8string = unicodestring.encode("utf-8")
Related