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)
She*_*ena 38
字符串已经"解码",因此str类没有'decode'函数.因此:
AttributeError: type object 'str' has no attribute 'decode'
Run Code Online (Sandbox Code Playgroud)
如果要解码字节数组并将其转换为字符串调用:
the_thing.decode(encoding)
Run Code Online (Sandbox Code Playgroud)
如果要对字符串进行编码(将其转换为字节数组),请调用:
the_string.encode(encoding)
Run Code Online (Sandbox Code Playgroud)
就base 64的东西而言:使用'base64'作为上面编码的值会产生错误:
LookupError: unknown encoding: base64
Run Code Online (Sandbox Code Playgroud)
打开控制台并输入以下内容:
import base64
help(base64)
Run Code Online (Sandbox Code Playgroud)
您将看到base64有两个非常方便的功能,即b64decode和b64encode.b64 decode返回一个字节数组,b64encode需要一个字节数组.
要将字符串转换为它的base64表示形式,首先需要将其转换为字节.我喜欢utf-8但是使用你需要的任何编码......
import base64
def stringToBase64(s):
return base64.b64encode(s.encode('utf-8'))
def base64ToString(b):
return base64.b64decode(b).decode('utf-8')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
44692 次 |
| 最近记录: |