在这个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在base 64中编码?
谢谢