python3Unicode和字符实体转中文

Unicode转中文

如果type(text) is bytes

1
text.decode('unicode_escape')

如果type(text) is str

1
text.encode('latin-1').decode('unicode_escape')

字符实体转中文

1
2
import html
print(html.unescape('a=1&b=2'))

错误

1
2
3
from html.parser import HTMLParser
print(HTMLParser().unescape('a=1&b=2'))
#报一下错误

DeprecationWarning: The unescape method is deprecated and will be removed in 3.5, use html.unescape() instead.

原因:python3.4以后已经弃用

解决: 用 html 模块中的 unescape 方法

1
2
import html
print(html.unescape('a=1&b=2'))

参考