Python 编码和系统编码问题。 - 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
fxxkgw

Python 编码和系统编码问题。

  •  
  •   fxxkgw 2017 年 4 月 14 日 2607 次点击
    这是一个创建于 3298 天前的主题,其中的信息可能已经有所发展或是发生改变。

    情形 1 : LC_ALL="en_US.UTF-8"

    >>>i=u'呵呵'

    >>>i

    u'\u5475\u5475'

    >>>i.encode('utf-8')

    '\xe5\x91\xb5\xe5\x91\xb5'

    >>>type(i)

    <type 'unicode'>

    情形 2 : LC_ALL=C

    >>> i=u'呵呵'

    >>> i

    u'\xe5\x91\xb5\xe5\x91\xb5' #这是什么鬼??

    >>> type(i)

    <type 'unicode'>

    >>> i.encode('utf-8')

    '\xc3\xa5\xc2\x91\xc2\xb5\xc3\xa5\xc2\x91\xc2\xb5'

    唯一的区别就是 LC_ALL 了,所以谁能详细解释下这个编码与 LC_ALL 的关系呢。

    第 1 条附言    2017 年 4 月 15 日
    结个贴。。
    以前确实没太仔细研究编码和 export 出得 LC_*系列参数 昨晚仔细 Google 了一遍。

    1 、查看系统默认编码 sys.getdefaultencoding( ) 一般为 ascii

    2 、在终端获取系统的输入、输出编码格式 sys.stdin.encoding sys.stdout.encoding 正常应该为 utf-8 设置方法为 export PYTHOnIOENCODING=UTF-8

    3 、 u ’中文’=‘中文’.decode(encode)
    此处 encode 值为 sys.stdin.encoding
    所以当为 utf-8 时 '中文'.decode('utf-8 ’)=u'\u4e2d\u6587 ’
    当为 ASCII 时 '中文'.decode('ISO-8859-1 ’)=u'\xe4\xb8\xad\xe6\x96\x87 ’

    4 、 os.path.exists(path) 当 path 里有中文路径时,尽量转成 utf-8 后再和英文路径相加

    5 、 print 输出时候尽量要 encode(‘ utf-8 ’)

    https://wiki.archlinux.org/index.php/Locale_(简体中文)

    https://segmentfault.com/a/1190000004357933

    http://www.w2bc.com/article/216391

    http://stackoverflow.com/questions/2596714/why-does-python-print-unicode-characters-when-the-default-encoding-is-ascii

    http://blog.csdn.net/liuyukuan/article/details/50855748
    8 条回复    2017-04-16 04:19:51 +08:00
    zhihaofans
        1
    zhihaofans  
       2017 年 4 月 15 日 via iPhone
    →python3
    coolair
        2
    coolair  
       2017 年 4 月 15 日 via Android
    .decode('gbk')
    fy
        3
    fy  
       2017 年 4 月 15 日
    我认为是你输入的值有了问题,不然你看看 len(i) 是怎样?
    wwqgtxx
        4
    wwqgtxx  
       2017 年 4 月 15 日 via iPhone
    快转换到 python3 吧,别在编码问题上死磕了
    SuT2i
        5
    SuT2i  
       2017 年 4 月 15 日
    Python3 没有这些问题。。
    dant
        6
    dant  
       2017 年 4 月 15 日
    LC_ALL=C 时, Python 不知道你输入的字面量是什么编码,于是默认 ISO-8859-1 。
    encode 的时候,就按 ISO-8859-1 -> UTF-8 的规则转换了。
    dant
        7
    dant  
       2017 年 4 月 15 日
    纠正一下,是解析 u'呵呵' 的时候把 “呵呵” 的 UTF-8 表示( E5 91 B5 E5 91 B5 )当作 ISO-8859-1 编码转换为 Unicode codepoint 序列( U+00E5 U+0091 U+00B5 U+00E5 U+0091 U+00B5 )了.
    encode 的时候,就是把上面提到的那个 Unicode codepoint 序列编码成 UTF-8
    lzjun
        8
    lzjun  
       2017 年 4 月 16 日
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2435 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 43ms UTC 16:05 PVG 00:05 LAX 09:05 JFK 12: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