gpt4 book ai didi

python - base64.encodestring 在 python 3 中失败

转载 作者:太空狗 更新时间:2023-10-29 17:12:37 26 4
gpt4 key购买 nike

以下代码在 python 2 机器上成功运行:

base64_str = base64.encodestring('%s:%s' % (username,password)).replace('\n', '')

我正在尝试将它移植到 Python 3,但是当我这样做时遇到以下错误:

>>> a = base64.encodestring('{0}:{1}'.format(username,password)).replace('\n','')
Traceback (most recent call last):
File "/auto/pysw/cel55/python/3.4.1/lib/python3.4/base64.py", line 519, in _input_type_check
m = memoryview(s)
TypeError: memoryview: str object does not have the buffer interface

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/auto/pysw/cel55/python/3.4.1/lib/python3.4/base64.py", line 548, in encodestring
return encodebytes(s)
File "/auto/pysw/cel55/python/3.4.1/lib/python3.4/base64.py", line 536, in encodebytes
_input_type_check(s)
File "/auto/pysw/cel55/python/3.4.1/lib/python3.4/base64.py", line 522, in _input_type_check
raise TypeError(msg) from err
TypeError: expected bytes-like object, not str

我尝试搜索 encodestring 用法的示例,但找不到合适的文档。我错过了一些明显的东西吗?我在 RHEL 2.6.18-371.11.1.el5 上运行这个

最佳答案

您可以encode() 字符串(将其转换为字节字符串),然后再将其传递给base64.encodestring。示例 -

base64_str = base64.encodestring(('%s:%s' % (username,password)).encode()).decode().strip()

关于python - base64.encodestring 在 python 3 中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31144988/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com