zoukankan      html  css  js  c++  java
  • TypeError: expected string or bytes-like object

    在写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='

    >>>

     

     

    clip_image001

  • 相关阅读:
    & 微信支付对比
    # MySQL性能优化技巧
    & mysql简单定时增量全量备份
    & Mysql高级总结
    python面向对象
    django虚拟环境的安装
    Python 内置函数
    Python列表解析式
    函数练习
    Python装饰器
  • 原文地址:https://www.cnblogs.com/kerrycode/p/11382048.html
Copyright © 2011-2022 走看看