首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >python中的StopIteration错误

python中的StopIteration错误
EN

Stack Overflow用户
提问于 2016-02-22 17:41:23
回答 1查看 3.2K关注 0票数 0

我正在使用wolframalphawit.ai,我试图构建从wit.ai音频而不是终端文本获取数据的wolframalpha

我的代码是:

代码语言:javascript
复制
#!/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)

我面临这个错误:

代码语言:javascript
复制
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?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-22 18:19:57

当生成器耗尽且没有更多的值时,就会引发StopIteration,因此完全可以得到一个值。但你得自己处理:

代码语言:javascript
复制
try:
    print(next(res.results).text)
except StopIteration:
    print("No more suggesstions.")
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35560290

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档