'bytes'对象没有'encode'属性

DEV*_*911 15 python bcrypt python-3.x

在将每个文档插入集合之前,我正在尝试存储salt和哈希密码.但在编码salt和密码时,它显示以下错误:

 line 26, in before_insert
 document['salt'] = bcrypt.gensalt().encode('utf-8')

AttributeError: 'bytes' object has no attribute 'encode'
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

def before_insert(documents):
    for document in documents:
        document['salt'] = bcrypt.gensalt().encode('utf-8')
        password = document['password'].encode('utf-8')
        document['password'] = bcrypt.hashpw(password, document['salt'])
Run Code Online (Sandbox Code Playgroud)

我在virtualenv中使用eve框架和python 3.4

小智 5

您正在使用:

bcrypt.gensalt()
这个方法似乎生成了一个字节对象。这些对象没有任何编码方法,因为它们只适用于 ASCII 兼容数据。所以你可以尝试不使用.encode('utf-8')

python 3文档中的字节描述