Python 转换为基64时,TypeError:';str';不支持缓冲区接口

Python 转换为基64时,TypeError:';str';不支持缓冲区接口,python,base64,Python,Base64,当我试图将从图像读取的数据编码到base64时,它返回一个错误: im = Image.open(filePath) # load image self.msg = str(bytearray(list(im.getdata()))) # convert image data to string encodedMsg = base64.b64encode(self.msg) 当我在家使用Ubuntu(Python2.7)时,它就可以工作了。但当我使用学校

当我试图将从图像读取的数据编码到base64时,它返回一个错误:

im = Image.open(filePath)                     # load image
self.msg = str(bytearray(list(im.getdata()))) # convert image data to string
encodedMsg = base64.b64encode(self.msg)
当我在家使用Ubuntu(Python2.7)时,它就可以工作了。但当我使用学校机器(python3.4)时,它显示了错误。我怎样才能解决这个问题

File "Steganography.py", line 42, in msgToXml
    encodedMsg = base64.b64encode(self.msg)
  File "/opt/python3/current/lib/python3.4/base64.py", line 62, in b64encode
    encoded = binascii.b2a_base64(s)[:-1]
TypeError: 'str' does not support the buffer interface

参考资料:

简言之,这是因为在Python3中,unicode/string/bytes系统进行了重大改革。你应该读这本书,了解发生了什么,以及如何处理


为了回答您的具体问题,如果您不将bytearray转换为str,一切都应该正常。

我从xml文件中读取了一些数据。此代码
self.msg=base64.b64解码(self.xml.encode('utf-8'))
返回b64解码返回binascii.a2b_base64(s)binascii中的第90行
文件/opt/python3/current/lib/python3.4/base64.py。错误:填充不正确
encodedMsg = base64.b64encode(self.msg.encode('ascii'))