简体   繁体   English

获取类型错误TypeError:需要类似字节的object,而不是'str

[英]Getting type error TypeError: a bytes-like object is required, not 'str

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' message = message.replace(escape, "\"+escape) TypeError: 需要类似字节的 object,而不是 'str'

This code is working fine in Python 2 but getting above error in python 3此代码在 Python 2 中运行良好,但在 python 3 中出现上述错误

The error message indicates that the replace method expects bytes for the first parameter, not a string which you have passed.错误消息表明replace方法需要字节作为第一个参数,而不是您传递的字符串。 Syntactically you can specify a bytes literal by prefixing a bytes string with b .从语法上讲,您可以通过在字节字符串前加上b来指定字节文字。

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

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.这解决了您的错误,您正在使用blob= b'...'中的 b 属性将字符串定义为类字节,但是将 function 替换为字符串 arguments 只接受字符串。

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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:需要一个类似字节的对象,而不是'str':得到此错误 - TypeError: a bytes-like object is required, not 'str' : Getting this error TypeError:需要一个类似字节的对象,而不是“ str”,但类型是“ bytes” - TypeError: a bytes-like object is required, not 'str' but type is 'bytes' TypeError:需要类似字节的 object,不是“str”,但类型显示字节 - TypeError: a bytes-like object is required, not 'str' but type shows bytes TypeError:需要一个类似字节的对象,而不是“str” - TypeError: a bytes-like object is required, not 'str' TypeError:需要类似字节的 object,而不是“str”? - TypeError: a bytes-like object is required, not 'str'? TypeError:需要一个类似字节的对象,而不是'str' - TypeError: a bytes-like object is required, not 'str' 类型错误:需要类似字节的 object 而不是“str” - TypeError: a bytes-like object is required not 'str' 需要一个类似字节的对象,而不是'str':TypeError - a bytes-like object is required, not 'str' : TypeError TypeError: bytes-like object 是必需的,而不是 'str' - TypeError: bytes-like object is required, not 'str' 出现错误“TypeError: a bytes-like object is required, not 'str'”,但为什么呢? - Error "TypeError: a bytes-like object is required, not 'str'" appears, but why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM