1. “UnicodeDecodeError: ‘gbk’ codec can’t decode

with open(filename, 'r') as f:
    data = f.read()

将其改写为:

with open(filename, 'rb') as f:
    data = f.read()

但这样读取到的文件内容,不可直接读取。为了阅读的方便,可进一步为其指定编码类型:

with open(filename, 'r', encoding='utf-8') as f:
    data = f.read()
posted on 2018-01-18 00:05  未雨愁眸  阅读(343)  评论(0编辑  收藏  举报