我需要将图像(或任何文件)转换为 base64 字符串。我使用不同的方式,但结果总是 byte
,而不是字符串。例子:
import base64
file = open('test.png', 'rb')
file_content = file.read()
base64_one = base64.encodestring(file_content)
base64_two = base64.b64encode(file_content)
print(type(base64_one))
print(type(base64_two))
回来
<class 'bytes'>
<class 'bytes'>
我如何获得一个字符串,而不是字节?蟒蛇 3.4.2。
原文由 Vladimir37 发布,翻译遵循 CC BY-SA 4.0 许可协议
Base64 是一种 ascii 编码,所以你可以用 ascii 解码