![图片[1]-python中unicode编码转换为中文-uusu优素-乐高,模型,3d打印,编程](http://uusucn.zbbe.cn/wp-content/uploads/2024/01/1608255105591517.png)
我们使用python中,遇到爬取网站情况,用到unicode编码,我们需要将它转换为中文,unicode编码转换为中文的方法有四种:使用unicode_escape 解码、使用encode()方法转换,再调用bytes.decode()转换为字符串形式、使用json.loads 解码(为json 格式)、使用eval(遇到Unicode是通过requests在网上爬取的时候)。具体内容请看本文。
方法一:使用unicode_escape 解码
unicode=b'\\u4f60\\u597d'
re=unicode.decode("unicode_escape")
print(re)
返回:你好
方法二:使用encode()方法转换,再调用bytes.decode()转换为字符串形式
s=r'\u4f60\u597d'
print(s.encode().decode("unicode_escape"))
方法三: 使用json.loads 解码(为json 格式)
str='\u4eac\u4e1c\u653e\u517b\u7684\u722c\u866b'
printjson.loads('"%s"'%str)
方法四:使用eval(遇到Unicode是通过requests在网上爬取的时候)
response=requests.get(url,headers=headers)
re=eval("u"+"\'"+response.text+"\'")
print(re)原文来自:https://www.py.cn                    © 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
    



















































 
        

暂无评论内容