简体   繁体   中英

TypeError: expected bytes, bytearray or buffer compatible object

I'm trying to port a Python2 script (which works perfectly) to Python3, but I have a problem.

This is my code:

def encode_script(duck_text, duck_lang, bunny=None):

    lang_dir = os.path.join(os.path.dirname(__file__), 'languages')
    language_dict = os.path.join(lang_dir, '{0}.json'.format(duck_lang))
    lang_file = json.load(open(language_dict))
    encoded_file = parse_text(duck_text, lang_file, bunny)

    if encoded_file and not bunny:
        if 'Not in Language' in encoded_file:
            return encoded_file
        else:
            try:
                encoded_file = "".join(encoded_file)
                duck_blob = io.StringIO()

                duck_blob.write(encoded_file.decode('hex'))

                duck_bin = duck_blob.getvalue()
                duck_blob.close()
                return duck_bin

            except Exception as e:
                print ("Error creating inject.bin: {0}".format(e))
                return False

and this is the error:

D:\encdec\Encoders-decoders\DuckToolkit-master>python ducktools.py -e -l us test.txt ducky.bin
[+] Reading Input file.
  [-] Encoding File
Traceback (most recent call last):
  File "ducktools.py", line 58, in <module>
    duck_bin = encoder.encode_script(duck_text, language)
  File "D:\encdec\Encoders-decoders\DuckToolkit-master\ducktoolkit\encoder.py", line 160, in encode_script
    encoded_file = parse_text(duck_text, lang_file, bunny).decode()
  File "D:\encdec\Encoders-decoders\DuckToolkit-master\ducktoolkit\encoder.py", line 24, in parse_text
    duck_text = duck_text.replace("\r", "")
TypeError: expected bytes, bytearray or buffer compatible object

The script works perfect on Python2. What should I do?

I know this is a bit late but I was having the same issue when converting a statement with .replace() from python2 to python3 and was able to fix it by prepending 'b' before the arguments in .replace() so try changing:

duck_text = duck_text.replace("\r", "")

to:

duck_text = duck_text.replace(b"\r", b"")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM