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

Python3 是真的比 Python2 爽多了。。

  •  
  •   PureWhite ·
    PureWhiteWu · 2018-02-13 00:09:01 +08:00 · 8807 次点击
    这是一个创建于 2256 天前的主题,其中的信息可能已经有所发展或是发生改变。

    自己用 Django 写了个后台,Python3 完全不用管字符编码 ,是真的爽。

    31 条回复    2018-02-23 17:18:58 +08:00
    di94sh
        1
    di94sh  
       2018-02-13 02:22:31 +08:00 via Android
    只要吧 Python 的默认编码 asiic 编码换成 utf-8 就好了。
    import sys
    reload ( sys )
    sys.setdefaultcdfing('utf-8')
    di94sh
        2
    di94sh  
       2018-02-13 02:24:23 +08:00 via Android
    @di94sh 手机打字费劲,,,setdefaultencoding
    Dillion
        3
    Dillion  
       2018-02-13 08:35:49 +08:00 via iPhone   ❤️ 1
    我记得这个方法不是不推荐用么
    ctro15547
        4
    ctro15547  
       2018-02-13 09:16:31 +08:00   ❤️ 1
    @di94sh
    并不能解决所有问题
    yangyaofei
        5
    yangyaofei  
       2018-02-13 09:25:11 +08:00 via Android   ❤️ 2
    别着急,会出现爸爸的儿子怎么变成你的问题
    xavierskip
        6
    xavierskip  
       2018-02-13 09:43:15 +08:00
    我告诉你依旧会遇到字符编码的问题。
    Rob007
        7
    Rob007  
       2018-02-13 09:51:48 +08:00
    Python2 的字符问题只能缓解,不能根除
    laoyur
        8
    laoyur  
       2018-02-13 09:58:54 +08:00   ❤️ 2
    python 渣路过
    如果单纯只是编码问题
    py2 里用:
    # -*- coding: utf-8 -*-
    from __future__ import unicode_literals

    不是已经能解决大部分问题了么?
    beforeuwait
        9
    beforeuwait  
       2018-02-13 10:11:16 +08:00
    好像是的,我写都不会加 #coding=utf8
    Zzde
        10
    Zzde  
       2018-02-13 10:13:38 +08:00 via Android
    python3 django 遇到过字符串编码问题,终端疯狂报错,网站 500。
    booty
        11
    booty  
       2018-02-13 10:54:08 +08:00
    python3 好像还有很多库不支持?
    l1093178
        12
    l1093178  
       2018-02-13 10:55:02 +08:00
    @di94sh 你这是在强力作死...
    l1093178
        13
    l1093178  
       2018-02-13 10:55:53 +08:00
    @laoyur 有些地方由于解释器本身的限制不能用 unicode,比如异常的错误信息,比如 __all__ 里面导出的符号名称
    xiaxiaocao
        14
    xiaxiaocao  
       2018-02-13 11:22:51 +08:00   ❤️ 2
    @laoyur Python2 的编码问题主要是
    1. 会做 unicode 和 str 的隐式转换
    2. 转换用的不是 UTF8,而是 default local,不同环境表现会不一样

    所以在不知不觉间就埋下了各种坑
    dexter
        15
    dexter  
       2018-02-13 13:07:02 +08:00   ❤️ 3
    low 啊
    python 简直一门任性无药可救 又慢又 2B 的语言
    ycz0926
        16
    ycz0926  
       2018-02-13 14:45:08 +08:00
    如果是 web 开发的话,python 3 ( 3.6 )的 django、tornado 已经支持的很不错了,只遇到过一个阿里 sms 库的兼容问题,不过也在 github 找到了 python 3 的版本
    ycz0926
        17
    ycz0926  
       2018-02-13 14:46:31 +08:00   ❤️ 1
    python 3 有一个略繁琐的操作是,往 tonrado 的 socket 中写数据,必须得是 byte 类型的,每次都得 encode 一下
    ycz0926
        18
    ycz0926  
       2018-02-13 14:48:22 +08:00
    @dexter 目前来说,python 其实可以满足很大一部分的需求了,如果是高并发或者涉及到系统方面的需求,可以考虑 go
    ycz0926
        19
    ycz0926  
       2018-02-13 14:50:23 +08:00
    @l1093178 你难不成把 class 或 function 写成中文了?😊
    laoyuan
        20
    laoyuan  
       2018-02-13 15:04:04 +08:00
    Python 3 在 web、爬虫方面的编码问题,我总结如下:

    '发' == '\u53d1'
    r'发' == '\\u53d1' == json.dumps('发').strip('"')
    '发'.encode('unicode-escape') == b'\\u53d1' == r'发'.encode("utf-8")
    SingeeKing
        21
    SingeeKing  
       2018-02-13 15:36:11 +08:00
    我问一个无关问题。。。Python 2 的编码声明到底是什么原理

    # coding: utf-8
    # _*_ coding:utf-8 -*-
    # coding=utf-8

    都能用,而且 utf-8 和 utf8 也是一样的效果(并且 Python 中所有的字符串传入都可以通用)

    所以编码声明到底是什么原理?为什么书写有这么多形式
    twor
        22
    twor  
       2018-02-13 15:39:13 +08:00   ❤️ 2
    @SingeeKing
    If a comment in the first or second line of the Python script matches the regular expression coding[=:]\s*([-\w.]+), this comment is processed as an encoding declaration; the first group of this expression names the encoding of the source code file. The encoding declaration must appear on a line of its own. If it is the second line, the first line must also be a comment-only line.

    https://stackoverflow.com/questions/41680533/is-coding-utf-8-also-a-comment-in-python
    twor
        23
    twor  
       2018-02-13 15:41:17 +08:00
    @SingeeKing 可能没有 get 到你到问题,忽略吧
    SingeeKing
        24
    SingeeKing  
       2018-02-13 15:53:18 +08:00
    @twor #23 完美回答了我的问题!谢谢!
    loading
        25
    loading  
       2018-02-13 16:10:43 +08:00 via iPhone
    我在 py2 学了编码,到 go 学到了类型…
    julyclyde
        26
    julyclyde  
       2018-02-14 08:26:42 +08:00
    @booty python3 不支持的都是你不需要用的库
    aminic
        27
    aminic  
       2018-02-14 13:02:31 +08:00 via Android
    只有 rust 才能解救编码问题
    qq976739120
        28
    qq976739120  
       2018-02-14 13:35:33 +08:00
    没用的啦,工作的话还是会要求你用 2 的
    likuku
        29
    likuku  
       2018-02-14 19:40:52 +08:00
    @booty

    Python 3 Wall of Superpowers : http://python3wos.appspot.com/

    绝大部分库都已经支持 py3
    shamashii
        30
    shamashii  
       2018-02-20 16:50:03 +08:00
    endecode 低于折磨后的人来说,准确地讲是 3.6 版本开始好的
    frostming
        31
    frostming  
       2018-02-23 17:18:58 +08:00
    reload(sys)这方法总感觉脏,还会有其他问题出现

    还是要有一个好习惯,除了输入输出,其余全部 unicode
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5563 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 01:34 · PVG 09:34 · LAX 18:34 · JFK 21:34
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.