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

UnicodeDecodeError: 'utf8' codec can't decode byte 0xcc in position 0: invalid continuation byte

  •  
  •   mingyun · 2015-09-04 23:52:38 +08:00 · 8540 次点击
    这是一个创建于 3372 天前的主题,其中的信息可能已经有所发展或是发生改变。
    windows python2.7 执行 pip -h 然后报错如何,怎么破?
    $ pip -h
    Traceback (most recent call last ):
    File "d:\python2\Scripts\pip-script.py", line 9, in <module>
    load_entry_point ('pip==7.1.2', 'console_scripts', 'pip')()
    File "d:\python2\lib\site-packages\distribute-0.6.49-py2.7.egg\pkg_resources.p
    y", line 345, in load_entry_point
    return get_distribution (dist ).load_entry_point (group, name )
    File "d:\python2\lib\site-packages\distribute-0.6.49-py2.7.egg\pkg_resources.p
    y", line 2381, in load_entry_point
    return ep.load ()
    File "d:\python2\lib\site-packages\distribute-0.6.49-py2.7.egg\pkg_resources.p
    y", line 2087, in load
    entry = __import__(self.module_name, globals (),globals (), ['__name__'])
    File "d:\python2\lib\site-packages\pip-7.1.2-py2.7.egg\pip\__init__.py", line
    15, in <module>
    from pip.vcs import git, mercurial, subversion, bazaar # noqa
    File "d:\python2\lib\site-packages\pip-7.1.2-py2.7.egg\pip\vcs\mercurial.py",
    line 10, in <module>
    from pip.download import path_to_url
    File "d:\python2\lib\site-packages\pip-7.1.2-py2.7.egg\pip\download.py", line
    38, in <module>
    from pip._vendor import requests, six
    File "d:\python2\lib\site-packages\pip-7.1.2-py2.7.egg\pip\_vendor\requests\__
    init__.py", line 53, in <module>
    from .packages.urllib3.contrib import pyopenssl
    File "d:\python2\lib\site-packages\pip-7.1.2-py2.7.egg\pip\_vendor\requests\pa
    ckages\__init__.py", line 3, in <module>
    from . import urllib3
    File "d:\python2\lib\site-packages\pip-7.1.2-py2.7.egg\pip\_vendor\requests\pa
    ckages\urllib3\__init__.py", line 10, in <module>
    from .connectionpool import (
    File "d:\python2\lib\site-packages\pip-7.1.2-py2.7.egg\pip\_vendor\requests\pa
    ckages\urllib3\connectionpool.py", line 31, in <module>
    from .connection import (
    File "d:\python2\lib\site-packages\pip-7.1.2-py2.7.egg\pip\_vendor\requests\pa
    ckages\urllib3\connection.py", line 9, in <module>
    from http.client import HTTPConnection as _HTTPConnection, HTTPException
    File "d:\python2\http.py", line 2, in <module>
    from CGIHTTPServer import CGIHTTPRequestHandler
    File "d:\python2\lib\CGIHTTPServer.py", line 30, in <module>
    import SimpleHTTPServer
    File "d:\python2\lib\SimpleHTTPServer.py", line 27, in <module>
    class SimpleHTTPRequestHandler (BaseHTTPServer.BaseHTTPRequestHandler ):
    File "d:\python2\lib\SimpleHTTPServer.py", line 208, in SimpleHTTPRequestHandl
    er
    mimetypes.init () # try to read system mime.types
    File "d:\python2\lib\mimetypes.py", line 368, in init
    db.read_windows_registry ()
    File "d:\python2\lib\mimetypes.py", line 268, in read_windows_registry
    for subkeyname in enum_types (hkcr ):
    File "d:\python2\lib\mimetypes.py", line 257, in enum_types
    ctype = ctype.encode (default_encoding ) # omit in 3.x!
    UnicodeDecodeError: 'utf8' codec can't decode byte 0xcc in position 0: invalid c
    ontinuation byte
    5 条回复    2015-09-06 14:07:27 +08:00
    matthewgao
        1
    matthewgao  
       2015-09-05 08:39:11 +08:00
    看起来很怪啊, 你可以看下他那个 pip-script 的脚本, 为什么 help 会涉及 simpleHTTPserver 。。。

    codec 这个问题,一般就是因为他以为他在 Decode UTF-8 的数据,但是实际上他不是 UTF-8 的
    mingyun
        2
    mingyun  
    OP
       2015-09-05 10:38:06 +08:00
    @matthewgao 那个文件就 10 行代码
    #!d:\python2\python.exe
    # EASY-INSTALL-ENTRY-SCRIPT: 'pip==1.5.2','console_scripts','pip'
    __requires__ = 'pip==1.5.2'
    import sys
    from pkg_resources import load_entry_point

    if __name__ == '__main__':
    sys.exit (
    load_entry_point ('pip==1.5.2', 'console_scripts', 'pip')()
    )

    和这个有关吗?
    >>> sys.getdefaultencoding ()
    'utf-8'
    xiazi
        3
    xiazi  
       2015-09-05 17:13:31 +08:00
    这样的情况一般都是 gbk 被当成 utf-8 decode 了.

    应该使用:
    import locale
    locale.getdefaultlocale ()[1]
    mingyun
        4
    mingyun  
    OP
       2015-09-05 21:17:22 +08:00
    @xiazi locale.getdefaultlocale ()[1]输出 cp936 该怎么处理呢?
    xiazi
        5
    xiazi  
       2015-09-06 14:07:27 +08:00
    升级到 python2.7.10
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1073 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 19:28 · PVG 03:28 · LAX 11:28 · JFK 14:28
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.