推荐学习书目
› 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
andy12530
V2EX  ›  Python

求助,关于python的list 中文乱码

  •  
  •   andy12530 · Nov 25, 2012 · 19886 views
    This topic created in 5097 days ago, the information mentioned may be changed or developed.
    在sublime text2的调试里面

    #coding=utf-8

    print "中国"
    # 这里输出为:中国

    L = ["中国", 'a', 'b'];
    print L;
    #这里输出为:['\xe4\xb8\xad\xe5\x9b\xbd', 'a', 'b']

    最简单的办法,搞定list里面的中文,让它正常输出。

    PS:
    每次弄编码都很头疼,大家全用UTF8不就完嘛。搞这么复杂
    15 replies  •  2015-12-23 15:24:27 +08:00
    skydiver
    skydiver
    Nov 25, 2012
    1
    这个就是utf8编码吧,只不过是按照16进制显示的
    andy12530
    andy12530
    OP
    Nov 25, 2012
    2
    @skydiver 为虾米它会把中文搞成16进制的呢,有木有最简单的方法,把它转为正常的。
    cabbala
    cabbala
    Nov 25, 2012
    3
    @andy12530 列表不是让你整个print。。请用for i in list: print i
    timonwong
    timonwong
    Nov 25, 2012
    4
    @andy12530
    对于非string类型, print 输出的是用 __repr__() 表达后的。

    因此你不能直接用它,需要自己写一个打印该list
    yuelang85
    yuelang85
    Nov 25, 2012
    5
    L = [unicode("中国", 'utf-8'), 'a', 'b'];
    ehaagwlke
    ehaagwlke
    Nov 25, 2012
    6
    @cabbala 是不是for i in list: print str(i) 就可以了?
    timonwong
    timonwong
    Nov 26, 2012
    7
    @ehaagwlke
    在string list的情况下,直接print元素就可以了,要一次打印完,用join可能更好点:

    print ', '.join(L)
    cabbala
    cabbala
    Nov 26, 2012
    8
    @ehaagwlke print i就行了,没必要str(i),i已经是string了
    cabbala
    cabbala
    Nov 26, 2012
    9
    @timonwong
    @ehaagwlke join好用+1
    Niris
    Niris
    Nov 26, 2012
    10
    你需要的是 python3。
    andy12530
    andy12530
    OP
    Nov 26, 2012
    11
    python3 可以解决这个问题。

    @cabbala 一语中的。的确在程序中是不需要打印这个东西的。
    @timonwong 研究的很深入,学习了。
    Kai
    Kai
    Nov 26, 2012
    12
    moved to /go/python :)
    binux
    binux
    Nov 26, 2012
    13
    ergatea
    ergatea
    Nov 26, 2012
    14
    L = [u"中国", 'a', 'b'];
    qiuhanyuan
    qiuhanyuan
    Dec 23, 2015
    15
    如果是在 json 中的怎么办呢?虽然将这个 unicode 值取出来打印是中文,但是打印 json 字符串或者字典还是 /uxxxx 编码。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2127 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 46ms · UTC 14:35 · PVG 22:35 · LAX 06:35 · JFK 09:35
    ♥ Do have faith in what you're doing.