python解码base64
时间: 2023-07-22 08:09:14 浏览: 49
在Python中解码Base64可以使用`base64`模块的`b64decode()`函数。下面是一个示例代码:
```python
import base64
encoded_string = 'SGVsbG8gd29ybGQ='
decoded_string = base64.b64decode(encoded_string).decode('utf-8')
print(decoded_string)
```
运行这段代码,将输出解码后的字符串:"Hello world!"。在示例中,我们首先导入了`base64`模块,然后定义了一个经过Base64编码的字符串`encoded_string`。接下来,使用`b64decode()`函数对该字符串进行解码,并使用`decode()`方法将其转换为UTF-8编码的字符串。最后,我们打印出解码后的字符串。
请注意,在解码时需要指定正确的字符编码,通常是UTF-8。如果Base64编码的字符串中包含非UTF-8字符,则需要相应地更改解码时的字符编码。
相关问题
python 解码base64
以下是Python解码base64的方法:
```python
import base64
# 将字符串编码为base64格式
str = 'hello world'
str_b64 = base64.b64encode(str.encode('utf-8'))
print(str_b64) # 输出:b'aGVsbG8gd29ybGQ='
# 将base64格式解码为字符串
str_decode = base64.b64decode(str_b64).decode('utf-8')
print(str_decode) # 输出:hello world
```
python实现base64解码
Python中可以使用`base64`模块来实现Base64解码。以下是一个简单的示例:
```python
import base64
# 需要解码的Base64字符串
encoded_str = "aGVsbG8gd29ybGQ="
# 使用base64模块进行解码
decoded_bytes = base64.b64decode(encoded_str)
# 将解码后的字节转换为字符串
decoded_str = decoded_bytes.decode('utf-8')
print("解码后的字符串:", decoded_str)
```
在这个示例中,我们首先导入了`base64`模块。然后,我们定义了一个需要解码的Base64字符串`encoded_str`。接下来,我们使用`base64.b64decode()`函数对字符串进行解码,得到解码后的字节。最后,我们将解码后的字节转换为字符串并打印出来。