gpt4 book ai didi

python - 'TypeError : 'str' does not support the buffer interface' - python 3. 4 从 2.7 转换

转载 作者:太空宇宙 更新时间:2023-11-03 17:22:15 27 4
gpt4 key购买 nike

我尝试从 python 2.7 转换为 3.4,并在尝试使用 b64encode 时出现以下错误:

from base64 import b64encode

username = 'manager'
password = 'test.manager'

headers = {"Authorization": " Basic " + b64encode(username + ":" + password), "Content-Type": "application/json"}

错误:

Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\python34\Lib\base64.py", line 62, in b64encode
encoded = binascii.b2a_base64(s)[:-1]
**TypeError: 'str' does not support the buffer interface**

最佳答案

改用bytes对象:

username = b'manager'
password = b'test.manager'

headers = {"Authorization": b" Basic " + b64encode(username + b":" + password), "Content-Type": "application/json"}

关于python - 'TypeError : 'str' does not support the buffer interface' - python 3. 4 从 2.7 转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33022424/

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