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

关于 python 编码问题的疑惑(重新修改主题)

  •  
  •   gps32251070 · 2016-11-17 15:27:59 +08:00 · 2009 次点击
    这是一个创建于 2723 天前的主题,其中的信息可能已经有所发展或是发生改变。
    初学 python ,今天在使用 django 的时候遇到一些编码问题,希望大神指教下。

    我在访问 index 方法的时候不会报错,但是访问 news 方法的时候却报错,只有把第二个方法加上 u ,也就是这样才能正确输出

    我想问加上 u 之后 python 到底干了什么动作,为什么第一个 index 方法就可以正常输出?
    下面是报错信息
    第 1 条附言  ·  2016-11-18 14:44:19 +08:00
    我昨天发现貌似是因为 django 自动把传进来的参数进行了 unicode 操作,把代码改成
    return HttpResponse(u"新闻 ID 是:%s" % news_id)
    或者
    return HttpResponse("新闻 ID 是:%s" % news_id.encode("utf8"))
    就可以了
    7 条回复    2016-11-18 14:41:52 +08:00
    Geoion
        1
    Geoion  
       2016-11-17 15:51:21 +08:00
    unicode 能够正常输出啊
    Jblue
        2
    Jblue  
       2016-11-17 15:59:32 +08:00
    u''是 unicode 化。
    你这三个都是贴的一个。
    stamaimer
        3
    stamaimer  
       2016-11-17 17:17:44 +08:00 via iPhone
    在 python2 中,有两种字符串类型,一种是 str ,一种是 unicode

    创建 unicode 类型的字符串的方法就是在普通字符串前面加 u

    unicode 类型的字符串可以编码( encode )成某种编码的 str

    某种编码的 str 也可以解码( decode )成 unicode

    但是文档上说,在必要的时候可以提供这两种类型之间的自动类型转换,这也是为啥可以在 str 类型上执行 encode ,在 unicode 类型上执行 decode

    从出错信息上看,是在 decode 的过程中出错,但是说的是 ascii 不能解码这个序号,说明在你的环境里默认的是 ascii ,而不是 utf-8 ,你可以通过 sys.getdefaultencoding()来查看一下,通过 sys.setdefaultencoding(‘ utf8 ’)来设置,这样设置之后,不加 u 也不会出错。

    至于为啥第一个不报错,第二个报错,也就是为啥第一个不 decode ,而第二个 decode ,我猜是格式化字符串需要 unicode 类型的字符串导致的。
    stamaimer
        4
    stamaimer  
       2016-11-17 23:15:44 +08:00
    是这样的,所有链接字符串的操作的操作数都需要是 unicode ,所以第二个操作需要把“新闻 ID 是:%s ”隐式执行 decode 操作,但是隐式 decode 的编码使用的是默认编码 ascii ,所以出错。详细解释参见 http://nedbatchelder.com/text/unipain.html
    freestyle
        5
    freestyle  
       2016-11-18 09:49:37 +08:00
    news 会报错是因为 "新闻 ID 是:%s"是 str,url 传进来的 news_id 是 unicode 你可以 type(news_id)看一下输出

    django 全部用 u''就没事。或者在顶部导入 unicode_literals 这样默认不加 u 就是 unicode 字符,加 b 是 str ( Py2 )
    # coding=utf-8
    from __future__ import unicode_literals
    NaVient
        6
    NaVient  
       2016-11-18 10:47:36 +08:00
    from __future__ import unicode_literals
    django 1.10 已经明确指出中文需要这句了
    gps32251070
        7
    gps32251070  
    OP
       2016-11-18 14:41:52 +08:00
    @stamaimer @freestyle
    我昨天发现是因为 django 自动把传进来的参数进行了 unicode 操作,把代码改成
    return HttpResponse(u"新闻 ID 是:%s" % news_id)
    或者
    return HttpResponse("新闻 ID 是:%s" % news_id.encode("utf8"))
    就可以了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   770 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 22:17 · PVG 06:17 · LAX 15:17 · JFK 18:17
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.