【问题标题】:Can the python string be changed by string finding?python字符串可以通过字符串查找来更改吗?
【发布时间】:2016-04-05 08:35:05
【问题描述】:
doc = open("1.html").read().strip()
doc = doc.decode("utf-8","ignore")

这个例子没问题。我可以得到正确的 unicode 字符串文档。

doc = open("1.html").read().strip()
if u"charset=utf" in doc or u"charset=\"utf" in doc:
    doc = doc.decode("utf-8","ignore")

出现错误“UnicodeDecodeError: 'ascii' codec can't decode byte 0xe7 in position 289: ordinal not in range(128)” 任何人都可以解释这个吗?字符串文档可以通过字符串查找来更改吗? 忘了说,1.html里面有中文。

【问题讨论】:

    标签: python string unicode encoding utf-8


    【解决方案1】:

    问题是您将从文件中读取的字节字符串与您的 unicode 文字字符串 u"charset=utf"u"charset=\"utf" 进行比较。为了比较它们,Python 必须在此时将字节字符串转换为 unicode - 在您手动调用 decode 之前 - 它使用默认的 ASCII 编解码器来执行此操作。

    解决方案是始终将字节串与字节串进行比较:

    if "charset=utf" in doc or "charset=\"utf" in doc:
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-11
      相关资源
      最近更新 更多