python - python中的StopIteration错误

标签 python linux python-3.x wolfram-mathematica speech-recognition

我正在同时使用 wolframalphawit.ai 我正在尝试构建 wolframalphawit.ai 获取数据 音频而不是终端文本。

我的代码是:

#!/usr/bin/python
import speech_recognition as sr
import wolframalpha
import sys

r = sr.Recognizer()
with sr.Microphone() as source:
    print("Say something!")
    audio = r.listen(source)


WIT_AI_KEY = "NQYEITRO5GL2Q2MZFIJE4UHWVNQEUROW"
try:
    print("Wit.ai thinks you said " + r.recognize_wit(audio, key=WIT_AI_KEY))
except sr.UnknownValueError:
    print("Wit.ai could not understand audio")
except sr.RequestError as e:
    print("Could not request results from Wit.ai service; {0}".format(e))


client = wolframalpha.Client('PR5756-H3EP749GGH')
print(r.recognize_wit(audio, key=WIT_AI_KEY))
res = client.query(r.recognize_wit(audio, key=WIT_AI_KEY))
print(next(res.results).text)

我遇到了这个错误:

MacBook-Air:Documents exepaul$ python ak.py
2016-02-22 23:05:04.429 Python[3003:122880] 23:05:04.428 WARNING:  140: This application, or a library it uses, is using the deprecated Carbon Component Manager for hosting Audio Units. Support for this will be removed in a future release. Also, this makes the host incompatible with version 3 audio units. Please transition to the API's in AudioComponent.h.
Say something!
Wit.ai thinks you said seven
seven
Traceback (most recent call last):
  File "ak.py", line 24, in <module>
    print(next(res.results).text)
StopIteration

如何将数据提供给 wolframalpha api?

最佳答案

StopIteration 在生成器耗尽且没有更多值时引发,获得一个完全没问题。但是你需要自己处理:

try:
    print(next(res.results).text)
except StopIteration:
    print("No more suggesstions.")

关于python - python中的StopIteration错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35560290/

相关文章:

用于向量化函数的 Python 和 Numba

linux - 需要一些帮助编写 shell 脚本来添加数字

python - 在 Python 中如何创建可变长度组合或排列?

python - pygame中的 Sprite 掩码碰撞问题

python追加字典到列表

python27从.bat打开程序并关闭.exe

python - 检查 pandas 数据框日期列的日期格式是否正确?

linux - 如何在 unix 中格式化带有目录名的文件列表?

linux - .c.s : in linux-0. 11 生成文件

python - 如何将字符串变量从一个函数传递到另一个函数?