TypeError: expected string or bytes-like object
程序员文章站
2022-04-14 15:42:05
在写Python代码的时候,遇到了“TypeError: a bytes-like object is required, not 'str'”错误,此处实验机器的Python环境为Python 3.6.6,如下所示 >>> import base64 >>> db_user_encode=base... ......
在写python代码的时候,遇到了“typeerror: a bytes-like object is required, not 'str'”错误,此处实验机器的python环境为python 3.6.6,如下所示
>>> import base64
>>> db_user_encode=base64.b64encode('kerry')
traceback (most recent call last):
file "<stdin>", line 1, in <module>
file "/usr/local/lib/python3.6/base64.py", line 58, in b64encode
encoded = binascii.b2a_base64(s, newline=false)
typeerror: a bytes-like object is required, not 'str'
上面错误“类型错误:需要类似字节的对象,而不是字符串”,在python3中:因为3.x中字符都为unicode编码,函数b64encode的参数的数据类型是bytes类型的字符串对象,而我们给的是str类型的变量,所以必须进行转码,如下所示:
>>> import base64
>>> db_user_encode=base64.b64encode(b'kerry')
>>> db_user_encode
b'a2vycnk='
>>>
下一篇: CSS的基础学习
推荐阅读
-
使用xadmin更新数据时,报错expected string or bytes-like object
-
解决报错:TypeError: argument should be integer or bytes-like object, not ‘str‘
-
编译安卓源码提示:TypeError: argument should be integer or bytes-like object, not ‘str‘
-
TypeError: expected string or bytes-like object
-
Python网络编程报错TypeError: a bytes-like object is required, not 'str' 的解决办法
-
使用xadmin更新数据时,报错expected string or bytes-like object
-
TypeError: expected string or bytes-like object
-
Python网络编程报错TypeError: a bytes-like object is required, not 'str' 的解决办法