“字节"对象没有属性“编码" [英] 'bytes' object has no attribute 'encode'

查看:87
本文介绍了“字节"对象没有属性“编码"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to store salt and hashed password before inserting each document into a collection. But on encoding the salt and password, it shows the following error:

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

AttributeError: 'bytes' object has no attribute 'encode'

这是我的代码:

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'])

我在带有python 3.4的virtualenv中使用eve框架

I'm using eve framework in virtualenv with python 3.4

推荐答案

您正在使用:

bcrypt.gensalt()

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

This method seems to generate a bytes object. These objects do not have any encode methods as they only work with ASCII compatible data. So you can try without .encode('utf-8')

Python 3文档中的字节描述

这篇关于“字节"对象没有属性“编码"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆