Python的open函数报错’gbk’ codec can’t decode byte

报错信息

代码如下:


with open("./test.html") as fin:
    html_doc = fin.read()

报错信息如下:

html_doc = fin.read()

UnicodeDecodeError: 'gbk' codec can't decode byte 0xa2 in position 108: illegal multibyte sequence

解决办法

在open的时候,指定编码

with open("./test.html", encoding="utf8") as fin:
    html_doc = fin.read()

即可解决

Leave a Comment