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

关于'ascii' codec can't encode characters in position 的问题

  •  
  •   chengxiao · Jan 12, 2018 · 5801 views
    This topic created in 3217 days ago, the information mentioned may be changed or developed.
    为什么已经用了 Python3 了 而且在头部加了# encoding=utf8
    还是会出现 ascii' codec can't encode characters in position 的报错

    快被这个问题搞疯了 ,Mac 本机测试正常,以 uwsgi 跑在 ubuntu 上就报错
    9 replies  •  2018-01-12 14:31:28 +08:00
    lhx2008
    lhx2008
    Jan 12, 2018 via Android
    1
    命令行可能不支持 unicode 输出,print 前先 encode
    chengxiao
    chengxiao
    OP
    Jan 12, 2018
    2
    @lhx2008 是 Django 里用的 ImageFiled, 只要上传中文名称的图片 就报错
    xawsolevo
    xawsolevo
    Jan 12, 2018
    3
    可以试试先 encode 到 latin 再 decode 为 utf8
    est
    est
    Jan 12, 2018
    4
    用的 windows ?
    chengxiao
    chengxiao
    OP
    Jan 12, 2018
    5
    @est 不啊 Ubuntu16.04 Nginx uwsgi python3.6
    privatez
    privatez
    Jan 12, 2018
    6
    import sys

    reload(sys)

    sys.setdefaultencoding('utf8')

    试试看 QAQ
    Arnie97
    Arnie97
    Jan 12, 2018 via Android❤️1
    7
    1. 请将$LANG 或$LC_ALL 设置为 en_US.utf8 或 zh_CN.utf8。
    2. 如果不存在上述的 locale,先用 locale-gen 生成。具体操作可以参考 https://help.ubuntu.com/community/Locale
    3. 如果不考虑 Python 2 的兼容性,不必加 encoding 头部。Python 3 会忽略这行注释,强制要求 UTF-8 源码。

    @est 楼主发帖时已经说了 Ubuntu ;并且 Windows 无法将代码页设置为 ASCII。

    @xawsolevo 就是因为你这种人才会发生 /t/421212 这种情况😂
    chengxiao
    chengxiao
    OP
    Jan 12, 2018
    8
    @Arnie97 多谢 我去尝试下 现在也是觉得问题出在系统环境这里
    chengxiao
    chengxiao
    OP
    Jan 12, 2018
    9
    @Arnie97 非常感谢 问题解决了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2994 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 12:06 · PVG 20:06 · LAX 04:06 · JFK 07:06
    ♥ Do have faith in what you're doing.