推荐学习书目
› 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
hcnhcn012
V2EX  ›  Python

Python 字符串编码的问题

  •  
  •   hcnhcn012 · Aug 5, 2017 via iPhone · 3239 views
    This topic created in 3339 days ago, the information mentioned may be changed or developed.
    python 中 unicode 和 str 到底有什么关系?为什么 Python3 中找不到 unicode 这个类,却可以用 u''来初始化变量?还是说一个 str 有多种编码方式,u'abc'就相当于'abc'.encode(),但是在 python3 中'abc'.encode('unicode')这种参数是不存在的,这是怎么回事?
    从 len('汉')=3,len(u'汉')=1 的结果看出,str 是默认 utf-8 编码的?
    zhihhh
        1
    zhihhh  
       Aug 5, 2017
    在 python3 里面所有的 str 都默认是 unicode 了。不存在有'abc'.endcode('unicode')这种说法了吧。
    gdsing
        2
    gdsing  
       Aug 5, 2017
    不清楚楼主到底是想问 py3,还是 py2。
    1、如楼上说的,py3 默认都是 unicode
    2、len('汉')=3 可能会出现 len('汉')=2 的情况,py2 str 编码是跟系统的。
    pia
        3
    pia  
       Aug 5, 2017
    还有'unicode'参数?是 'abc'.encode('utf-8') 吧
    hcnhcn012
        4
    hcnhcn012  
    OP
       Aug 5, 2017 via iPhone
    @gdsing 好的👌谢谢我知道了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2545 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 01:06 · PVG 09:06 · LAX 18:06 · JFK 21:06
    ♥ Do have faith in what you're doing.