Python 中使用 unicode_escape 编码中文乱码问题
在 Python 3 中,使用 'unicode_escape' 编码时,需要确保字符串本身是 Unicode 字符串。如果使用普通字符串(即字节串),则会导致中文乱码。以下是一个示例:
# 使用字节串
s = b'\u4e2d\u6587'
print(s.decode('unicode_escape'))
# 输出:中文
# 使用Unicode字符串
s = '\u4e2d\u6587'
print(s.encode('unicode_escape').decode())
# 输出:\u4e2d\u6587
可以看到,使用字节串时,解码后的结果是正确的,但是在使用 'unicode_escape' 编码的过程中,中文字符被转义了。而使用 Unicode 字符串时,编码后的结果是正确的,中文字符没有被转义。
原文地址: https://www.cveoy.top/t/topic/mOat 著作权归作者所有。请勿转载和采集!