【发布时间】:2016-02-27 18:36:52
【问题描述】:
我需要将图像(或任何文件)转换为 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'>
如何获取字符串而不是字节? Python 3.4.2。
【问题讨论】:
-
@AlastairMcCormack 我需要在文件中写入 base64 文本,然后再读取。
标签: python python-3.x base64