推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
redhatping
V2EX  ›  Python

一个小小的中文编码问题

  •  
  •   redhatping · Jun 13, 2015 · 3082 views
    This topic created in 4016 days ago, the information mentioned may be changed or developed.

    如,

    #coding:utf-8
    a = ['中国','美国']
    print a
    print a[1]

    结果是
    ['\xe4\xb8\xad\xe5\x9b\xbd', '\xe7\xbe\x8e\xe5\x9b\xbd']
    美国


    如何print a , 显示 ['中国','美国']


    为什么a[1],可以是中文,a 就是编码了呢?

    7 replies    2015-06-13 10:58:48 +08:00
    yahoo21cn
        1
    yahoo21cn  
       Jun 13, 2015
    print str(a).decode('string_escape')
    blueset
        2
    blueset  
       Jun 13, 2015
    用 Python3 测试了一下

    >>> a = ['中国','美国']
    >>> print (a)
    ['中国', '美国']
    redhatping
        3
    redhatping  
    OP
       Jun 13, 2015
    @blueset python2呢
    redhatping
        4
    redhatping  
    OP
       Jun 13, 2015
    @yahoo21cn 再请问一下,为什么是这样呢,a[1],是中文呢?
    yahoo21cn
        5
    yahoo21cn  
       Jun 13, 2015   ❤️ 1
    因为默认在list打印的是utf-8编码,你如果不想打印编码,而是显示字型,需要string_escape解码一下
    yetone
        6
    yetone  
       Jun 13, 2015 via iPhone
    试一下 from __future__ import unicode_literals
    redhatping
        7
    redhatping  
    OP
       Jun 13, 2015
    @yahoo21cn 谢谢 :)
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3151 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 44ms · UTC 13:37 · PVG 21:37 · LAX 06:37 · JFK 09:37
    ♥ Do have faith in what you're doing.