TypeError:需要一个类似字节的对象,而不是'str':收到此错误

时间:2019-05-07 06:27:01

标签: python python-3.x

获取错误:字节和str类型之间的冲突

old_server_uuid = p3.communicate()[0].split("|")[1].strip()

出现以下错误:

Traceback (most recent call last):
  File "create_env_file.py", line 68, in <module>
    data = create_env_source_list(node_name, ip_address)
  File "create_env_file.py", line 14, in create_env_source_list
    raise(ex)
  File "create_env_file.py", line 12, in create_env_source_list
    old_server_uuid = p3.communicate()[0].split("|")[1].strip()
TypeError: a bytes-like object is required, not 'str'

1 个答案:

答案 0 :(得分:1)

Python 3不允许混合文本(Unicode)字符串和字节字符串。

const IncompleteDogsIntentHandler = { // Occurs when the required slots are not filled canHandle(handlerInput) { return handlerInput.requestEnvelope.request.type === 'IntentRequest' && handlerInput.requestEnvelope.request.intent.name === 'DogIntent' && handlerInput.requestEnvelope.request.dialogState !== 'COMPLETED' }, async handle(handlerInput) { return handlerInput.responseBuilder .addDelegateDirective(handlerInput.requestEnvelope.request.intent) .getResponse(); } 的结果是一个字节字符串,因此p3.communicate()[0]参数也必须是一个。

示例:

用Unicode字符串分割字节字符串:

.split

用字节字符串分割字节字符串:

>>> b'abc|123'.split('|')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: a bytes-like object is required, not 'str'