UnicodeDecodeError: 'utf8' codec can't decode byte 0xcc in position 0: invalid continuation byte - V2EX
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 9062 次点击
    这是一个创建于 3726 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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     Solana     824 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 46ms UTC 21:46 PVG 05:46 LAX 13:46 JFK 16:46br /> Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86