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
theodorus
V2EX  ›  Python

关于 CLI 下打印中文字符问题。(并非伸手党)内置详细环境配置。希望有同学为我答疑解惑。那个贴描述不清, 下面添加详细信息。网络多为英文信息, 对中文没什么支持。

  •  
  •   theodorus · 2016-08-21 23:03:10 +08:00 · 1386 次点击
    这是一个创建于 2803 天前的主题,其中的信息可能已经有所发展或是发生改变。
    源码:
    [root@Debian 4test]#cat test.py
    #!/usr/bin/python
    # _*_ coding: utf-8 _*_

    print u"\u5929"

    环境 1:
    在 mac 自带终端下, 可以打印出字符非常顺利。 mac 的 terminal 是支持 UTF-8 和 GBK 或者其他乱七八糟的编码的。所以这也合情合理。

    环境 2:
    通过 ssh 连接 debian 可以再次运行, 会报错如下
    [root@Debian 4test]#./test.py
    Traceback (most recent call last):
    File "./test.py", line 5, in <module>
    print u"\u5929"
    UnicodeEncodeError: 'ascii' codec can't encode character u'\u5929' in position 0: ordinal not in range(128)
    搜索最后的报错信息, http://www.saltycrane.com/blog/2008/11/python-unicodeencodeerror-ascii-codec-cant-encode-character/, 个人认为解决方法复杂不好。

    环境详细配置:
    [root@Debian 4test]#locale
    locale: Cannot set LC_CTYPE to default locale: No such file or directory
    locale: Cannot set LC_ALL to default locale: No such file or directory
    LANG=en_US.UTF-8
    LANGUAGE=en_US:en
    LC_CTYPE=UTF-8
    LC_NUMERIC="en_US.UTF-8"
    LC_TIME="en_US.UTF-8"
    LC_COLLATE="en_US.UTF-8"
    LC_MONETARY="en_US.UTF-8"
    LC_MESSAGES="en_US.UTF-8"
    LC_PAPER="en_US.UTF-8"
    LC_NAME="en_US.UTF-8"
    LC_ADDRESS="en_US.UTF-8"
    LC_TELEPHONE="en_US.UTF-8"
    LC_MEASUREMENT="en_US.UTF-8"
    LC_IDENTIFICATION="en_US.UTF-8"
    LC_ALL=

    库文件信息
    [root@Debian 4test]#tree /usr/lib/locale
    /usr/lib/locale
    |-- C.UTF-8
    | |-- LC_ADDRESS
    | |-- LC_COLLATE
    | |-- LC_CTYPE
    | |-- LC_IDENTIFICATION
    | |-- LC_MEASUREMENT
    | |-- LC_MESSAGES
    | | `-- SYS_LC_MESSAGES
    | |-- LC_MONETARY
    | |-- LC_NAME
    | |-- LC_NUMERIC
    | |-- LC_PAPER
    | |-- LC_TELEPHONE
    | `-- LC_TIME
    `-- locale-archive

    2 directories, 13 files

    希望得到指点。我目前感觉是没有中文字体库的原因。我安装的是最小的(standard)版本。
    billlee
        1
    billlee  
       2016-08-21 23:48:21 +08:00
    建议在 python 下执行以下程序,看看输出

    ```python
    import os
    import sys


    print(sys.stdout.encoding)
    print({k: v for k, v in os.environ.iteritems() if k.startswith('LC') or k == 'LANG'})
    ```

    我猜你的环境下 sys.stdout.encoding 是有问题的
    adoal
        2
    adoal  
       2016-08-22 00:22:04 +08:00
    locale: Cannot set LC_CTYPE to default locale: No such file or directory
    locale: Cannot set LC_ALL to default locale: No such file or directory

    看起来像是系统的 locale 库有问题。你用
    dpkg-reconfigure locales
    重建一下 locale 库试试,记得选中 en_US.UTF-8
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3759 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 00:53 · PVG 08:53 · LAX 17:53 · JFK 20:53
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.