python中unicode编码转换为中文
发布时间:2025-11-18 11:16:36

当我们使用python时,当我们爬上网站并使用unicode编码时,我们需要将其转换为中文。将unicode编码转换为中文有四种方法:使用unicode_escape 解码、使用encode()转换方法,然后调用bytes.decode()转换为字符串形式,使用json.loads 解码(json 格式)、使用eval(遇到Unicode是通过requests在网上爬行的时候)。详见本文。
方法一:使用unicode_escape 解码
unicode=b'\u4f60\u597;
re=unicode.decode("unicode_escape")
print(re)
返回:你好方法二:使用encode()转换方法,然后调用bytes.decode()转换为字符串形式
s=r'u4f60\u597;
print(s.encode().decode("unicode_escape"))方法三: 使用json.loads 解码(为json 格式)
str='\u4eac\u4e1cu653e\u517b\u7684\u722c\u86b'
printjson.loads('"%s"'%str)方法四:使用eval(当Unicode通过requests在网上爬行时)
response=requests.get(url,headers=headers)
re=eval("u"+"\'"+response.text+"\'")
print(re)以上是将unicode编码转换成中文的方法,希望对你有所帮助~
