'str'不支持Python2中的缓冲接口Python3

'str'不支持Python2中的缓冲接口Python3

问题描述:

您在Py2中可以使用这两个功能,但是在Py3上却无法使用

Hi have this two funtions in Py2 works fine but it doesn´t works on Py3

def encoding(text, codes):
    binary = ''
    f = open('bytes.bin', 'wb')
    for c in text:
        binary += codes[c]
    f.write('%s' % binary)
    print('Text in binary:', binary)
    f.close()
    return len(binary)

def decoding(codes, large):
    f = file('bytes.bin', 'rb')
    bits = f.read(large)
    tmp = ''
    decode_text = ''
    for bit in bits:
        tmp += bit
        if tmp in fordecodes:
            decode_text += fordecodes[tmp]
            tmp = ''
    f.close()
    return decode_text

控制台输出:

Traceback (most recent call last):
  File "Practica2.py", line 83, in <module>
    large = encoding(text, codes)
  File "Practica2.py", line 56, in encoding
    f.write('%s' % binary)
TypeError: 'str' does not support the buffer interface

修复程序对我来说很简单

The fix was simple for me

使用

f = open('bytes.bin', 'w')

代替

f = open('bytes.bin', 'wb') 

您需要的是python 3 'w',而不是'wb'.

In python 3 'w' is what you need, not 'wb'.