【发布时间】:2019-04-14 05:09:15
【问题描述】:
在尝试将模块从 python 2 升级到使用 python 3 时,我在尝试对文件数据进行哈希处理时遇到类型错误,当我对数据进行编码时,我遇到了 TypeError “Unicode 对象必须在哈希之前进行编码”抛出 TypeError “只能将 str(不是“字节”)连接到 str”
with open(realPath, "rb") as fn:
while True:
filedata = fn.read(self.piece_length)
if len(filedata) == 0:
break
length += len(filedata)
##First error was here fixed with .decode()
data += filedata.decode('utf-8')
if len(data) >= self.piece_length:
info_pieces += sha1(data[:self.piece_length]).digest()
data = data[self.piece_length:]
if check_md5:
md5sum.update(filedata)
if len(data) > 0:
##New error happens here
info_pieces += sha1(data).digest()
【问题讨论】:
标签: python