【问题标题】:Python 2.7 , issue with decode('utf-8')Python 2.7,解码问题('utf-8')
【发布时间】:2013-11-11 15:53:10
【问题描述】:

我有:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from urllib2 import urlopen

page2 = urlopen('http://pogoda.yandex.ru/moscow/').read().decode('utf-8')

page = urlopen('http://yasko.by/').read().decode('utf-8')

并且在“page ...”行中出现错误“UnicodeDecodeError: 'utf8' codec can't decode byte 0xc3 in position 32: invalid continuation byte”,但在“page2 ...”行中没有错误,为什么?

从 yasko.by 中 32 的位置开始西里尔符号,我如何正确得到它?

谢谢!

【问题讨论】:

    标签: python utf-8


    【解决方案1】:

    http://yasko.by/的内容用windows-1251编码,http://pogoda.yandex.ru/moscow/的内容用utf-8编码。

    page = .. 行应该变成:

    page = urlopen('http://yasko.by/').read().decode('windows-1251')
    

    【讨论】:

    • 而不是 "Главная" 我有 "\u041e\u0428\u0418\u0411\u041a\u0410" 和 .decode('windows-第1251章)
    • @user2350206,非 ascii 字符在 Python 2.x 中表示为 u'\uxxxx' 形式。打印它会告诉你你所期望的:print(urlopen('http://yasko.by/').read().decode('windows-1251'))
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-30
    • 2018-02-18
    • 1970-01-01
    • 2018-06-06
    • 2018-06-17
    • 1970-01-01
    相关资源
    最近更新 更多