相关疑难解决方法(0)

为什么我需要'b'用Base64编码Python字符串?

在这个python示例之后,我将字符串编码为Base64,其中:

>>> import base64
>>> encoded = base64.b64encode(b'data to be encoded')
>>> encoded
b'ZGF0YSB0byBiZSBlbmNvZGVk'
Run Code Online (Sandbox Code Playgroud)

但是,如果我省略领先b:

>>> encoded = base64.b64encode('data to be encoded')
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python32\lib\base64.py", line 56, in b64encode
   raise TypeError("expected bytes, not %s" % s.__class__.__name__)
   TypeError: expected bytes, not str
Run Code Online (Sandbox Code Playgroud)

为什么是这样?

python base64 python-3.x

229
推荐指数
5
解决办法
36万
查看次数

如何从Base64转换为字符串Python 3.2

可能重复:
将字符串转换为Base 64和从Base 64转换字符串

def convertFromBase64 (stringToBeDecoded):
    import base64
    decodedstring=str.decode('base64',"stringToBeDecoded")
    print(decodedstring)
    return

convertFromBase64(dGhpcyBpcyBzdHJpbmcgZXhhbXBsZS4uLi53b3chISE=)
Run Code Online (Sandbox Code Playgroud)

我想要使​​用base64编码的字符串并将其转换回原始字符串,但我无法弄清楚到底出了什么问题

我收到了这个错误

 Traceback (most recent call last):
 File "C:/Python32/junk", line 6, in <module>
convertFromBase64(("dGhpcyBpcyBzdHJpbmcgZXhhbXBsZS4uLi53b3chISE="))
 File "C:/Python32/junk", line 3, in convertFromBase64
decodedstring=str.decode('base64',"stringToBeDecoded")
AttributeError: type object 'str' has no attribute 'decode'
Run Code Online (Sandbox Code Playgroud)

base64 python-3.x

9
推荐指数
1
解决办法
4万
查看次数

标签 统计

base64 ×2

python-3.x ×2

python ×1