【问题标题】:'utf8' codec can't decode byte 0xc3 while decode('utf-8') in python'utf8'编解码器在python中解码('utf-8')时无法解码字节0xc3
【发布时间】:2017-12-24 20:25:21
【问题描述】:

今天我的脚本中出现了奇怪的错误:

'utf8' codec can't decode byte 0xc3 in position 21: invalid continuation byte

我正在从套接字sock.recv 读取数据,结果是buff.decode('utf-8'),其中 buff 是返回的数据。

但是今天我发现了很多“独角兽”,其中一个字符返回“▒”

【问题讨论】:

    标签: python encoding utf-8


    【解决方案1】:

    .decode() 的第二个参数名为 errors。您可以将其设置为 'ignore' 以忽略所有非 utf8 字符,或将其设置为 'replace' 以将它们替换为菱形问号 (�)。

    buff.decode('utf-8', 'ignore')
    

    【讨论】:

    • 我爱你!正是我需要的
    猜你喜欢
    • 1970-01-01
    • 2020-07-17
    • 1970-01-01
    • 1970-01-01
    • 2019-11-10
    • 1970-01-01
    • 2017-11-05
    • 1970-01-01
    • 2011-12-13
    相关资源
    最近更新 更多