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

Python 字符串编码的问题

  •  
  •   hcnhcn012 · 2017-08-05 11:01:23 +08:00 via iPhone · 2017 次点击
    这是一个创建于 2448 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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  
       2017-08-05 11:34:35 +08:00
    在 python3 里面所有的 str 都默认是 unicode 了。不存在有'abc'.endcode('unicode')这种说法了吧。
    gdsing
        2
    gdsing  
       2017-08-05 15:05:56 +08:00
    不清楚楼主到底是想问 py3,还是 py2。
    1、如楼上说的,py3 默认都是 unicode
    2、len('汉')=3 可能会出现 len('汉')=2 的情况,py2 str 编码是跟系统的。
    pia
        3
    pia  
       2017-08-05 20:48:49 +08:00
    还有'unicode'参数?是 'abc'.encode('utf-8') 吧
    hcnhcn012
        4
    hcnhcn012  
    OP
       2017-08-05 21:59:54 +08:00 via iPhone
    @gdsing 好的👌谢谢我知道了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1212 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 23:21 · PVG 07:21 · LAX 16:21 · JFK 19:21
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.