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

Python 编码和系统编码问题。

  •  
  •   fxxkgw · 2017-04-14 20:38:50 +08:00 · 2107 次点击
    这是一个创建于 2562 天前的主题,其中的信息可能已经有所发展或是发生改变。

    情形 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-04-15 11:55:59 +08:00
    来结个贴。。
    以前确实没太仔细研究编码和 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-04-15 00:15:18 +08:00 via iPhone
    →python3
    coolair
        2
    coolair  
       2017-04-15 00:37:14 +08:00 via Android
    .decode('gbk')
    fy
        3
    fy  
       2017-04-15 00:54:17 +08:00
    我认为是你输入的值有了问题,不然你看看 len(i) 是怎样?
    wwqgtxx
        4
    wwqgtxx  
       2017-04-15 11:11:18 +08:00 via iPhone
    快转换到 python3 吧,别在编码问题上死磕了
    SuT2i
        5
    SuT2i  
       2017-04-15 21:13:59 +08:00
    Python3 没有这些问题。。
    dant
        6
    dant  
       2017-04-15 23:41:13 +08:00
    LC_ALL=C 时, Python 不知道你输入的字面量是什么编码,于是默认 ISO-8859-1 。
    encode 的时候,就按 ISO-8859-1 -> UTF-8 的规则转换了。
    dant
        7
    dant  
       2017-04-15 23:45:51 +08:00
    纠正一下,是解析 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-04-16 04:19:51 +08:00
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2616 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 15:17 · PVG 23:17 · LAX 08:17 · JFK 11:17
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.