错误:---只能将str(而不是“ bytes”)连接到str

时间:2019-12-20 09:48:48

标签: python-3.x jupyter-notebook ubuntu-18.04 importerror voice-recognition

我正在ubuntu上的jupyter笔记本中做语音助手,但出现错误:

can only concatenate str (not "bytes") to str

我的代码如下:

elif 'tell me about' in command:
        reg_ex = re.search('tell me about (.*)', command)
        try:
            if reg_ex:
                topic = reg_ex.group(1)
                ny = wikipedia.page(topic)
                sofiaResponse(ny.content[:100].encode('utf-8'))
        except Exception as e:
                print(e)
                sofiaResponse(e)

sofiaResponse('Hi User, I am Sofia and I am your personal voice assistant, Please give a command or say "help me" and I will tell you what all I can do for you.')#loop to continue executing multiple commands

while True:
   assistant(myCommand())

1 个答案:

答案 0 :(得分:1)

我猜您正在使用this article来定义:

def sofiaResponse(audio):
    "speaks audio passed as argument"
    print(audio)
    for line in audio.splitlines():
        os.system("say " + audio)

您多次调用此函数,但这是问题所在:

sofiaResponse(ny.content[:100].encode('utf-8'))

.encode()方法将str转换为bytes,但是您正在调用的函数期望str。如果"say " + audioaudio对象,当它bytes出现时,您将看到自己所看到的异常。

解决方案只是删除编码调用并将字符串通过:

sofiaResponse(ny.content[:100])

请下次您问一个问题时,是否可以复制并粘贴完整的追溯,这是错误消息的一部分。这将告诉您哪一行代码有问题以及从哪一行代码发出问题。仅仅提取最终的错误消息会使解决该问题变得更加困难。