V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2012-11-25 16:57:39 +08:00 · 18568 次点击
    这是一个创建于 4168 天前的主题,其中的信息可能已经有所发展或是发生改变。
    在sublime text2的调试里面

    #coding=utf-8

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

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

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

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

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

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

    @cabbala 一语中的。的确在程序中是不需要打印这个东西的。
    @timonwong 研究的很深入,学习了。
    Kai
        12
    Kai  
    MOD
       2012-11-26 13:47:31 +08:00
    moved to /go/python :)
    binux
        13
    binux  
       2012-11-26 13:53:15 +08:00
    ergatea
        14
    ergatea  
       2012-11-26 13:55:01 +08:00
    L = [u"中国", 'a', 'b'];
    qiuhanyuan
        15
    qiuhanyuan  
       2015-12-23 15:24:27 +08:00
    如果是在 json 中的怎么办呢?虽然将这个 unicode 值取出来打印是中文,但是打印 json 字符串或者字典还是 /uxxxx 编码。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5477 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 01:27 · PVG 09:27 · LAX 18:27 · JFK 21:27
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.