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

Python3 编码问题

  •  
  •   domino · 2016-11-15 01:12:31 +08:00 · 4625 次点击
    这是一个创建于 2691 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我的系统为繁体中文 Win7 64bit / Python35 (32bit)
    不知道为何下列程序,遇到简体,执行后会有错误.
    请问该怎解决呢?

    程序码

    from urllib.request import urlopen
    from bs4 import BeautifulSoup
    html = urlopen("http://home.baidu.com/")
    soup = BeautifulSoup(html , "lxml")
    print(soup.title);
    

    错误如下

    C:\py>hellokitty.py
    Traceback (most recent call last):
      File "C:\py\hellokitty.py", line 5, in <module>
        print(soup.title);
    UnicodeEncodeError: 'cp950' codec can't encode character '\u5173' in position 7:
     illegal multibyte sequence
    
    26 条回复    2016-11-16 17:18:26 +08:00
    jy02201949
        1
    jy02201949  
       2016-11-15 01:26:34 +08:00   ❤️ 1
    这种一般都是 windows 的锅, cmd 太蛋疼了,自己建个虚拟机跑吧
    hard2reg
        2
    hard2reg  
       2016-11-15 01:32:18 +08:00   ❤️ 1
    建议用 IDLE ,别用蛋疼的 cmd 。
    hard2reg
        3
    hard2reg  
       2016-11-15 01:33:32 +08:00
    或者先在 CMD 执行这个 chcp 65001
    Arthur2e5
        4
    Arthur2e5  
       2016-11-15 02:40:41 +08:00   ❤️ 2
    Windows 控制台默认使用目前系统编码,对于繁体中文是 Big5/cp950 。 Python 在 print 一个字符串之前,需要编码到目前 cmd 窗口使用的编码,而你遇到了个( Python 印象中的) Big5 没有的字符。(之所以强调可能是 Python 的错觉,是因为这个语言的官方实现对于编解码的实现经常拘泥“标准”到脱离现实的地步: cp936 直接指到 gbk 结果没有该有的欧元符号、 cp950 没有 HKSCS 支持……)

    你可以使用 chcp 65001 切到 utf-8 控制台窗口,当然也可以像楼上建议的一样走路绕开 Windows 控制台。
    TaMud
        5
    TaMud  
       2016-11-15 02:55:08 +08:00
    import

    try:
    reload(sys)
    sys.setdefaultencoding("utf-8")
    except:
    pass

    试一下看看
    XYxe
        6
    XYxe  
       2016-11-15 08:14:00 +08:00
    @TaMud Python3 没有 reload 吧
    jhaohai
        7
    jhaohai  
       2016-11-15 08:58:27 +08:00 via iPhone
    不要在 win 下搞这些,你会生不如死
    justou
        8
    justou  
       2016-11-15 09:09:12 +08:00
    如果不想去动 CMD, 且控制台输出仅做简单观测可以这样:

    # -*- encoding: utf-8 -*-

    import locale
    lc = locale.getpreferredencoding()

    s = 'GBK 控制台不能完全显示的表情(*●⁰♊⁰●)ノ'


    def show_msg(msg):
    # print(msg) # 注释掉后: UnicodeEncodeError: 'gbk' codec can't encode ...
    print(msg.encode(lc, 'ignore').decode(lc))

    show_msg(s) # GBK 控制台不能完全显示的表情(*●●)

    这将忽略掉本地编码不能显示的字符, 如果要精确记录就用 utf8 编码输出到文件
    init
        9
    init  
       2016-11-15 09:29:19 +08:00
    别用 win 写 python 我就遇到这个坑,没解决,后来又遇到许多安装包无法安装的坑,解决了一部分
    最后的解决方法
    装了个 ubuntu 。。。
    真心的装个 linux 吧不然后面会遇到更多的坑的
    domino
        10
    domino  
    OP
       2016-11-15 09:58:48 +08:00
    感谢各位协助~因刚买了一本网路采集在学 py,问题比较多.

    再请问下,难道 python 抓取的 html 含有简体 /繁体
    一定都得在 IDLE 上运行,才不会有编码的问题吗?

    不知道是否有没通用的方法,不用經過 chcp 65001 ,让 CMD 可以顺利显示各种语言.
    这样脚本给别人,在 win 运行也方便.
    mahone3297
        11
    mahone3297  
       2016-11-15 10:06:32 +08:00
    不是混充跨平台码?

    看到大家说换到 linux 上。我之前,在 win 上写脚本,脚本功能是想 move 一些文件。然后,目录是中文。
    问题:如果我的 php 脚本是 utf8 编码,那没效,因为目录名包含中文, win 上是 gbk 编码
    最后我的解决方案是,在 linux 上挂在 win 解决。大家有其他解决方案码?
    imn1
        12
    imn1  
       2016-11-15 10:20:04 +08:00
    “给别人用”本身就不方便,别人还要配相同 py 环境
    不同 win 的 codepage 编码就更加了

    通用方法
    1.统一环境开发
    2.程序内部做兼容转码
    3.统一使用环境( cmd 注册表改成 65001 ),但这个影响用户其他 cmd 使用
    4.win10/ bash on ubuntu on windows
    imn1
        13
    imn1  
       2016-11-15 10:23:17 +08:00   ❤️ 1
    @mahone3297
    我就是 win 从 php4 等到 php5.3 都没有 unicode 处理目录,才开始学 python 的
    Arnie97
        14
    Arnie97  
       2016-11-15 10:25:00 +08:00 via Android
    @TaMud @XYxe
    加上 from importlib import reload 基本上就是楼主需要的答案

    @justou 你说的方案在大多数场景下很好用,但不适合楼主。他的系统代码页是繁体,会导致很多简体字显示不出来,而不只是几个无关紧要的符号
    raysonx
        15
    raysonx  
       2016-11-15 10:42:41 +08:00
    Windows 默认命令行的锅,用 PowerShell 吧。
    mahone3297
        16
    mahone3297  
       2016-11-15 11:19:45 +08:00
    @imn1 所以, python 能完美解决这个问题是么?我去试试
    imn1
        17
    imn1  
       2016-11-15 11:28:21 +08:00   ❤️ 1
    @mahone3297
    补充一下,是 py3 , py3 读取多字节 win 路径是返回 utf-8 的,无需使用 win 的相关模块

    这个知识不知道是否过时:
    win 系统编码很复杂,例如 win 是 cp936(接近于 gbk)编码,如果一个路径含有中文、英文、韩文,它是一个混合编码,一般需要一些 win 相关的模块处理
    py3 能统一返回 utf-8
    qweweretrt515
        18
    qweweretrt515  
       2016-11-15 11:51:22 +08:00
    https://www.v2ex.com/t/320624 同病相怜,借地方求助下
    TaMud
        19
    TaMud  
       2016-11-15 14:34:10 +08:00
    关键的一句
    sys.setdefaultencoding("utf-8")
    剩下的自已查手册文 档就行了
    jy02201949
        20
    jy02201949  
       2016-11-15 14:57:19 +08:00   ❤️ 1
    @qweweretrt515 我觉得在 win 下调试代码最好还是用虚拟机, bash on ubuntu on win 应该也能解决这个问题,不过我没用过,好多人用过都说不错,可以在 win 上跑很多 linux 的东西了
    imcocc
        21
    imcocc  
       2016-11-15 15:46:03 +08:00 via iPhone
    @domino 你是下载个 pycharm 社区版的 免费。功能够用。 能省很多事。
    domino
        22
    domino  
    OP
       2016-11-15 20:26:30 +08:00
    @imcocc 感谢,目前使用 IDLE (Python 3.5 32-bit).
    Arthur2e5
        23
    Arthur2e5  
       2016-11-16 02:21:09 +08:00
    @domino chcp 65001 *就是* 你在找的通用方法,句号。

    @mahone3297 人生苦短,多手动用 u"你好中文"(当然用 __future__ unicode_literals 也是好事。)
    practicer
        24
    practicer  
       2016-11-16 10:46:05 +08:00
    解决响应文本编码问题, 用 requests 包更方便

    import requests
    from bs4 import BeautifulSoup
    html = requests.get("http://home.baidu.com/")
    soup = BeautifulSoup(html.content.decode('utf-8'), "lxml")
    print(soup.title);
    domino
        25
    domino  
    OP
       2016-11-16 15:33:50 +08:00
    @practicer
    这还是不能,
    你的系统是简体,你去抓个繁体网页看看.
    应该会出错.
    Septembers
        26
    Septembers  
       2016-11-16 17:18:26 +08:00
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2850 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 11:42 · PVG 19:42 · LAX 04:42 · JFK 07:42
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.