我需要将图像(或任何文件)转换为base64字符串。我使用不同的方式,但结果总是byte
,而不是字符串。例如:将文件转换为Python 3上的base64字符串
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文本文件,然后以后读它。 – Vladimir37