Python 的 List 如果有中文的话, 会印出 \xe4\xb8... 等等的编码(如下所示), 要如何印出中文呢? 

>>> a = ['中文', 'ab']
>>> print a
['\xe4\xb8\xad\xe6\x96\x87', 'ab']


下述列出几种作法:
1.使用 decode('string_escape') 来达成


>>> a = ['中文', 'ab']
>>> print a
['\xe4\xb8\xad\xe6\x96\x87', 'ab']
>>> print str(a).decode('string_escape')
['中文', 'ab']


2.使用 uniout 来达成


安装: sudo pip install uniout # Source code: https://github.com/moskytw/uniout
>>> a = ['中文', 'ab']
>>> import uniout
>>> print a
['中文', 'ab']




3.直接取用 _uniout 


从上述 uniout Project 直接取用 _uniout.py


>>> a = ['中文', 'ab']
>>> import _uniout
>>> print _uniout.unescape(str(a), 'utf8')
['中文', 'ab']

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-09-08
  • 2021-08-02
  • 2022-12-23
  • 2021-06-19
  • 2021-07-30
猜你喜欢
  • 2022-01-15
  • 2021-12-05
  • 2021-07-02
  • 2022-12-23
  • 2021-07-02
  • 2022-01-20
相关资源
相似解决方案