【发布时间】:2016-04-24 16:40:25
【问题描述】:
我正在尝试下载经过 BVLC 训练的模型,但遇到此错误
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 110: invalid start byte
我认为是因为下面的函数 (complete code)
# Closure-d function for checking SHA1.
def model_checks_out(filename=model_filename, sha1=frontmatter['sha1']):
with open(filename, 'r') as f:
return hashlib.sha1(f.read()).hexdigest() == sha1
知道如何解决这个问题吗?
【问题讨论】:
-
错误信息很清楚。要么您的文件根本不是 UTF8,要么已损坏。
-
这就是我尝试打印时得到的结果
f<_io.TextIOWrapper name='models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel' mode='r' encoding='utf8'> -
有趣。那么当您明确指定文件编码时会发生什么?
open(filename, 'r', encoding='utf8')之类的东西? -
我试图用这个
with open(filename, 'r', encoding='utf8') as f:修改第二行,但我得到了同样的错误 -
不,不告诉 Python 它是 UTF8。除非您确定它应该是 - 但 Python 告诉您它是 not 有效的 UTF8,而是别的东西。用一个好的代码编辑器打开文件,看看里面有什么。