python base64解码
时间: 2023-08-15 20:13:09 浏览: 120
要在Python中解码Base64编码,可以使用标准库中的`base64`模块。下面是一个示例代码:
```python
import base64
encoded_data = "SGVsbG8gd29ybGQh"
decoded_data = base64.b64decode(encoded_data).decode('utf-8')
print(decoded_data)
```
在上面的代码中,我们首先导入了`base64`模块。然后,我们将要解码的Base64编码数据存储在`encoded_data`变量中。使用`base64.b64decode()`函数对编码数据进行解码,并使用`.decode('utf-8')`方法将解码后的字节数据转换为字符串。最后,我们将解码后的数据打印出来。
上述代码的输出结果应该是:"Hello world!"。请确保提供正确的Base64编码数据进行解码。
相关问题
pythonbase64解码
要进行 Python 中的 Base64 解码操作,可以使用 base64 模块。具体实现可以参考下面的代码示例:
```python
import base64
encoded_str = 'dGhpc19kYXRhX2VuY29kZWQ='
decoded_str = base64.b64decode(encoded_str).decode('utf-8')
print(decoded_str)
```
上述代码中,先将待解码的字符串 `dGhpc19kYXRhX2VuY29kZWQ=` 传递给 `b64decode()` 方法进行 Base64 解码。然后再通过 `decode()` 方法将解码后的字节流转换为字符串。
输出结果为:`this_data_encoded`
python base64解码文件
可以使用Python内置的base64模块来解码文件。具体步骤如下:
1.首先,打开要解码的文件并读取其内容。
2.将读取的内容进行base64解码。
3.将解码后的内容写入新文件中。
下面是一个示例代码,演示如何使用Python解码base64编码的文件:
```python
import base64
# 打开要解码的文件并读取其内容
with open('encoded_file.txt', 'r') as f:
encoded_data = f.read()
# 将读取的内容进行base64解码
decoded_data = base64.b64decode(encoded_data)
# 将解码后的内容写入新文件中
with open('decoded_file.txt', 'wb') as f:
f.write(decoded_data)
```
在上面的代码中,我们首先打开要解码的文件并读取其内容。然后,我们使用base64.b64decode()函数对读取的内容进行解码。最后,我们将解码后的内容写入新文件中。