头疼!请问在 CLI 中怎么设置语言变量打印出\u5929 这中用[Python u"\u5929"] 表示的汉字。求教一下。在网上搜的乱七八糟的。 - V2EX
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 中怎么设置语言变量打印出\u5929 这中用[Python u"\u5929"] 表示的汉字。求教一下。在网上搜的乱七八糟的。

  •  
  •   theodorus 2016-08-21 22:10:21 +08:00 3066 次点击
    这是一个创建于 3396 天前的主题,其中的信息可能已经有所发展或是发生改变。
    LC_ALL="zh_CN.UTF-8"?
    我用的是debian。弄得我好辛苦= =
    est
        1
    est  
       2016-08-21 22:15:04 +08:00
    赞头像。。。
    theodorus
        2
    theodorus  
    OP
       2016-08-21 22:16:19 +08:00
    @est 别别别。人是丢了, 学点东西回去就可以了 233
    tomczhen
        3
    tomczhen  
       2016-08-21 22:19:50 +08:00
    你为什么不先去问一下神奇海螺呢?

    编码问题是基础啊,就算你问到了 python 解决方法的代码,不了解编码到底是怎么回事,还是会在其他地方遇到编码的坑的。这种大部分人都遇到过的坑,自己动手搜索一下都一大堆文章和资料。
    billlee
        4
    billlee  
       2016-08-21 22:20:38 +08:00
    试了下 print(u"\u5929"), 出来的就是中文啊
    billlee
        5
    billlee  
       2016-08-21 22:21:42 +08:00
    youKnowDai
        6
    youKnowDai  
       2016-08-21 22:23:02 +08:00
    echo $LANG
    export LANG="zh_CN.UTF-8"?
    theodorus
        7
    theodorus  
    OP
       2016-08-21 22:25:45 +08:00
    @tomczhen 我当然问啦。我上了谷歌, 鸟哥的站。我用了那个 dpkg-reconfigure locales 可是不行啊。我编码理解还好。主要是不知道怎么设置啊。
    theodorus
        8
    theodorus  
    OP
       2016-08-21 22:26:23 +08:00
    @billlee 假如你的 LANG 和 LC_ALL 是 en_US.UTF-8 的话会报错。
    theodorus
        9
    theodorus  
    OP
       2016-08-21 22:27:53 +08:00
    [root@Debian 4test]#export LANG=zh_CN.UTF-8
    [root@Debian 4test]#./test.py
    Traceback (most recent call last):
    File "./test.py", line 3, in <module>
    print u"\u5929"
    UnicodeEncodeError: 'ascii' codec can't encode character u'\u5929' in position 0: ordinal not in range(128)
    [root@Debian 4test]#


    我还试了 LC_ALL 等。还有 dpkg-reconfigure locales 等命令。可是不行啊。。。感觉要安装中文字体库?
    theodorus
        10
    theodorus  
    OP
       2016-08-21 22:30:53 +08:00
    @billlee 得得得。既然都这么说, 那我还是再试试吧。这玩意困扰我很久了。我用的是 minial 版的。
    billlee
        11
    billlee  
       2016-08-21 22:33:10 +08:00
    @theodorus 我复现不了。这个理论上和 en_US/zh_CN 是没有关系的,只要编码是 UTF-8, 终端字体支持中文,就可以显示
    theodorus
        12
    theodorus  
    OP
       2016-08-21 22:37:55 +08:00
    @billlee 没错我终端是 ssh 连接。然后编码如下
    [root@Debian 4test]#locale -a
    locale: Cannot set LC_CTYPE to default locale: No such file or directory
    locale: Cannot set LC_MESSAGES to default locale: No such file or directory
    locale: Cannot set LC_COLLATE to default locale: No such file or directory
    C
    C.UTF-8
    POSIX
    en_US.utf8


    我的终端是 mac 的 terminal 已经设置为支持中文的 GBK, UTF-8 等格式编码。能够显示中文。使用 ls 命令可以看到中文文件名称。也可输入。

    但是 python 无法解析这个让我很头疼。
    theodorus
        13
    theodorus  
    OP
       2016-08-21 22:42:24 +08:00
    @billlee 我应该发现问题了。麻烦你们了。应该是我必须设置 python 为 utf-8 编码。否则会自动设为 ascii 编码。我去多学习学习, 不随便发帖了。抱歉各位。
    only0jac
        14
    only0jac  
       2016-08-21 22:42:41 +08:00 via Android
    @billlee 用 py 的话,如果字符串变量中包含 unicode 字符,该怎么转回来?谢谢
    Sasasu
        15
    Sasasu  
       2016-08-21 22:44:47 +08:00 via Android
    文件里 /etc/locale.gen 加
    en_US.UTF-8 UTF-8
    zh_CN.UTF-8 UTF-8
    zh_TW.UTF-8 UTF-8

    运行
    locale-gen
    theodorus
        16
    theodorus  
    OP
       2016-08-21 22:48:56 +08:00
    @Sasasu 我将 python 文件设置为 utf-8 编码之后并且也按照你说的做了。可是还是无法打印出汉字。不知为何。在 mac 自带的 terminal 里面可以打出汉字。通过 ssh 连接 debian 里面却不能。我不知为何, 请问你能否告诉我原因吗?
    exoticknight
        17
    exoticknight  
       2016-08-21 22:51:37 +08:00
    windows , utf-8 格式,亲测 cmd 里面都行, code page 936
    #!/usr/bin/env python
    # _*_ coding: utf-8 _*_

    print(u"\u5929")
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2620 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 24ms UTC 15:05 PVG 23:05 LAX 07:05 JFK 10:05
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86