Python读取文件中的字符串已经是unicode编码,如:\u53eb\u6211,需要转换成中文时有两种方式

1.使用eval:

eval("u"+"\'"+unicodestr+"\'")

2.使用decode:

str1 = '\u4f60\u597d'  
print str1.decode('unicode_escape')  
你好 

unicodestr.decode('unicode_escape')  # 将转义字符\u读取出来

# ’\u’开头就基本表明是跟unicode编码相关的,“\u”后的16进制字符串是相应汉字的utf-16编码。Python里decode()和encode()为我们提供了解码和编码的方法。其中decode('unicode_escape')能将此种字符串解码为unicode字符串。


 
                    
            
                

相关文章:

  • 2022-12-23
  • 2021-09-02
  • 2021-08-05
  • 2021-10-26
  • 2022-12-23
  • 2021-10-03
  • 2022-12-23
  • 2022-01-04
猜你喜欢
  • 2022-12-23
  • 2021-12-06
  • 2022-12-23
  • 2021-09-29
  • 2021-09-05
  • 2022-12-23
相关资源
相似解决方案