V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
nankingpython
V2EX  ›  问与答

求助为什么 linux 的编码这么难搞?

  •  
  •   nankingpython · 2016-06-24 03:53:13 +08:00 · 2374 次点击
    这是一个创建于 2868 天前的主题,其中的信息可能已经有所发展或是发生改变。

    #coding=utf-8 import re import urllib.request html = urllib.request.urlopen("http://study.163.com/").read().decode('utf-8') print(type(html)) print(html)

    报错 UnicodeEncodeError: 'ascii' codec can't encode characters in position 595-599: ordinal not in range(128) 如果不出现中文可以输出,出现中文就报错, windows 就没有这个问题。 请问下如何解决? 谷歌能搜的搜了。。

    9 条回复    2016-06-24 17:00:59 +08:00
    msg7086
        1
    msg7086  
       2016-06-24 05:49:21 +08:00
    下一次记得把标题里的 linux 改成 python ……
    nankingpython
        2
    nankingpython  
    OP
       2016-06-24 06:48:19 +08:00
    @msg7086 忘写了。。。:P
    echo1937
        3
    echo1937  
       2016-06-24 07:02:27 +08:00 via iPhone
    下一次改成 python 2
    nankingpython
        4
    nankingpython  
    OP
       2016-06-24 07:08:50 +08:00
    @echo1937 不是 python3 好点吗,默认是 utf8
    hanxiV2EX
        5
    hanxiV2EX  
       2016-06-24 08:07:03 +08:00 via iPhone
    不是 Linux 的锅,是 pyhton
    EPr2hh6LADQWqRVH
        6
    EPr2hh6LADQWqRVH  
       2016-06-24 08:22:47 +08:00
    从网上读下来的数据属于二进制数据, Bytes, 不要预设它是文本数据,你不知道它真正的编码是啥
    NxnXgpuPSfsIT
        7
    NxnXgpuPSfsIT  
       2016-06-24 08:30:56 +08:00
    print(html.encode(sys.stdin.encoding, 'ignore'))
    imn1
        8
    imn1  
       2016-06-24 10:42:23 +08:00
    linux 才容易
    你试试在简体 windows ,建一个韩文目录,然后用 python 以外语言去读一下?
    nankingpython
        9
    nankingpython  
    OP
       2016-06-24 17:00:59 +08:00
    @avastms python3 urllib2 默认的是 str 吧
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   797 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 21:30 · PVG 05:30 · LAX 14:30 · JFK 17:30
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.