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

求个问题, Python 编码问题。

  •  
  •   CodeDrift · Apr 8, 2015 · 4285 views
    This topic created in 4072 days ago, the information mentioned may be changed or developed.

    用的ExcelResponse 输出Excel

    url路径为这个 user_export_down/?key=de4bfe042d193118a3f7887918ba8253&name=河南省%20郑州市%20中原中学%20用户数据统计[学生]

    chrome可以正常下载Excel,IE报错
    UnicodeEncodeError at user_export_down/
    'gbk' codec can't encode character u'\ufffd' in position 0: illegal multibyte sequenceRequest Method: GET

    请问要怎么处理。谢谢了。

    2 replies    2015-04-08 17:20:56 +08:00
    aaaa007cn
        2
    aaaa007cn  
       Apr 8, 2015
    看起来是 ie 发送请求时编码有问题
    导致 django 在 request.GET 中 decode 到 unicode 时出错
    \ufffd 就是著名的“锟斤拷”

    正确的处理方法应该是想办法让 ie 发送正确编码的 url
    比如正确设置页面编码等等

    或者在 views.py 中 try except
    当有编码错误时,手动设置 HttpRequest.encoding 后再次尝试
    https://docs.djangoproject.com/en/1.8/ref/request-response/#django.http.HttpRequest.encoding
    或者直接手动处理 HttpRequest.META['QUERY_STRING']
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3250 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 5414617a · 39ms · UTC 14:02 · PVG 22:02 · LAX 07:02 · JFK 10:02
    ♥ Do have faith in what you're doing.