小编Bad*_*ggX的帖子

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

im = Image.open(filePath)                     # load image
self.msg = str(bytearray(list(im.getdata()))) # convert image data to string
encodedMsg = base64.b64encode(self.msg)
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

当我在家使用Ubuntu(python 2.7)时,它可以工作.但是当我使用学校机器时它显示错误(python3.4).我怎么解决这个问题?

python base64

5
推荐指数
1
解决办法
7757
查看次数

打印字符串和浮动时出错

if __name__ == "__main__":
    fptr = open(sys.argv[1], 'r')
    for line in fptr:
        list1 = []
        s = ''
        for item in re.findall(r'[\S]+', line):
            try:
                list1.append(int(item))
            except:
                s = s + item + ' '
        if not len(list1) == 0:
            avg = sum(list1) / len(list1)
            print(list1)
            print(s)
            print(avg)
            print("{0:.3f} {}".format(avg, s)) //ERROR OCCUR
Run Code Online (Sandbox Code Playgroud)

这个标准:

[12, 14, 5, 20]
From sample set A
12.75
Traceback (most recent call last):
  File "./parse.py", line 28, in <module>
    print("{0:.3f} {}".format(avg, s))
ValueError: cannot switch from …
Run Code Online (Sandbox Code Playgroud)

python printing format

3
推荐指数
1
解决办法
4820
查看次数

标签 统计

python ×2

base64 ×1

format ×1

printing ×1