Home > Net >  Getting type error TypeError: a bytes-like object is required, not 'str
Getting type error TypeError: a bytes-like object is required, not 'str

Time:03-23

def escape_characters(message):
    for escape in ESCAPE_TTS_CHARACTERS:
        message = message.replace(escape, "\\" escape)
    for escape in ESCAPE_EDI_CHARACTERS:
        message = message.replace(escape, "\\" escape)
    for hexa in HEXA_TTS_CHARACTERS:
        message = message.replace(hexa, "\\x"  format(ord(hexa), "x"))
    for hexa in HEXA_EDI_CHARACTERS:
        message = message.replace(hexa, "\\x"  format(ord(hexa), "x"))
    message = message.replace("\x00", "\\x00")
    return message
ESCAPE_TTS_CHARACTERS = ["\\", "{", "}"]
HEXA_TTS_CHARACTERS = ["$", "%"]
ESCAPE_EDI_CHARACTERS = ["'", "*"]
HEXA_EDI_CHARACTERS = ["&", " ", "#", "\"", ":"]    

blob=b'\n\x11\n\x06TmgTty\x10\x00\x1a\x05\n\x03SIM\n\x16\n\x05Event\x10\x00\x1a\x0b\n\tDeparture\n\x12\n\x04Unit\x10\x00\x1a\x08\n\x06Minute\n\x11\n\x05Value\x10\x00\x1a\x06\n\x04-976\n\r\n\x07RefCity\x10\x00\x1a\x00\n\n\n\x04Date\x10\x00\x1a\x00\n\n\n\x04Time\x10\x00\x1a\x00\n\x0c\n\x06DftRul\x10\x00\x1a\x00\n\x0e\n\x05TiaId\x10\x00\x1a\x03\n\x011\n\x10\n\x07LastUid\x10\x00\x1a\x03\n\x011\n\x0b\n\x05Cabin\x10\x00\x1a\x00\n\x0e\n\x08BkgClass\x10\x00\x1a\x00\n\x0c\n\x06TgType\x10\x00\x1a\x00\n\r\n\x07TgValue\x10\x00\x1a\x00'

print(escape_characters(blob))

Getting below error

message = message.replace(escape, "\" escape) TypeError: a bytes-like object is required, not 'str'

This code is working fine in Python 2 but getting above error in python 3

CodePudding user response:

The error message indicates that the replace method expects bytes for the first parameter, not a string which you have passed. Syntactically you can specify a bytes literal by prefixing a bytes string with b.

message = message.replace(b'0', "\\x00")

CodePudding user response:

This solves your error, your are defining your string as bytes-like with the b attribute in blob= b'...' , but the replace function with string arguments only accepts strings.

def escape_characters(message):
    for escape in ESCAPE_TTS_CHARACTERS:
        message = message.replace(escape, "\\" escape)
    for escape in ESCAPE_EDI_CHARACTERS:
        message = message.replace(escape, "\\" escape)
    for hexa in HEXA_TTS_CHARACTERS:
        message = message.replace(hexa, "\\x"  format(ord(hexa), "x"))
    for hexa in HEXA_EDI_CHARACTERS:
        message = message.replace(hexa, "\\x"  format(ord(hexa), "x"))
    message = message.replace("\x00", "\\x00")
    return message
ESCAPE_TTS_CHARACTERS = ["\\", "{", "}"]
HEXA_TTS_CHARACTERS = ["$", "%"]
ESCAPE_EDI_CHARACTERS = ["'", "*"]
HEXA_EDI_CHARACTERS = ["&", " ", "#", "\"", ":"]    

blob='\n\x11\n\x06TmgTty\x10\x00\x1a\x05\n\x03SIM\n\x16\n\x05Event\x10\x00\x1a\x0b\n\tDeparture\n\x12\n\x04Unit\x10\x00\x1a\x08\n\x06Minute\n\x11\n\x05Value\x10\x00\x1a\x06\n\x04-976\n\r\n\x07RefCity\x10\x00\x1a\x00\n\n\n\x04Date\x10\x00\x1a\x00\n\n\n\x04Time\x10\x00\x1a\x00\n\x0c\n\x06DftRul\x10\x00\x1a\x00\n\x0e\n\x05TiaId\x10\x00\x1a\x03\n\x011\n\x10\n\x07LastUid\x10\x00\x1a\x03\n\x011\n\x0b\n\x05Cabin\x10\x00\x1a\x00\n\x0e\n\x08BkgClass\x10\x00\x1a\x00\n\x0c\n\x06TgType\x10\x00\x1a\x00\n\r\n\x07TgValue\x10\x00\x1a\x00'

print(escape_characters(blob))
  • Related