2016-04-02 30 views
0

我是新來的Wolfram | Alpha API for python,我無法在互聯網上找到太多幫助,所以我轉向了堆棧溢出。我在Wolfram | Alpha的一些查詢中收到「NameError:name'pod'未定義」。任何幫助將非常感激。當我輸入我的查詢「法拉利458的長度」時,我用StopIteration錯誤結束,現在我更改了代碼以使用「pod」方法。現在我得到一個NameError。輸出應該給我的車長(https://www.wolframalpha.com/input/?i=length+of+ferrari+458)我不得不x_出app_id,因爲它不是我的,抱歉給您帶來的不便。Wolfram | Alpha API Python NameError:名稱'pod'未定義

#!/usr/bin/python 
import wolframalpha 
app_id=('xxxxxx-xxxxxxxxxx') 
client = wolframalpha.Client(app_id) 

query = input("Query:") 
if len(res.pods) > 0: 
texts = "" 
pod = res.pods[1] 

if pod.text: 
    texts = pod.text 

else: 
    texts = "I have no answer for that" 

texts = texts.encode('ascii', 'ignore') 
print (texts) 

的錯誤,我得到:

Query: length of ferrari 458 
    Traceback (most recent call last): 
    File "Wolfram.py", line 24, in <module> 
     if pod.text: 
    NameError: name 'pod' is not defined 
+1

的'res.results'發生器是空的。我對API不熟悉,也許'res.pods'共享同一個生成器? –

+0

請更新您的問題,以添加您預期的輸出*。 –

+0

[文檔](https://pypi.python.org/pypi/wolframalpha)建議您需要使用* one *或* other *,而不是兩種技術。 –

回答

0

如果序列爲空next引發一個異常。

通過None作爲默認返回的第二個參數。從鏈接documentation

next(iterator[, default])
Retrieve the next item from the iterator by calling its next() method. If default is given, it is returned if the iterator is exhausted, otherwise StopIteration is raised.

如果沒有結果,你不想來處理異常:

first = next(res.results, None) 
if first: 
    print(first.text) 

如果沒有結果,firstNone,你可以再在嘗試使用它之前檢查。

+0

是的,'next()'可以返回一個默認值,但是這並不能解決沒有結果的問題。然後你會在'.text'上得到一個屬性錯誤。 –

+0

該問題被提出爲「StopIteration」問題,而不是空的「結果」屬性問題。 –

+0

我希望能夠查詢沒有任何明顯的錯誤發生。例如我的輸入:「法拉利458的最高速度」,我希望它在沒有StopIteration的情況下返回。因爲我是新手,我不太理解,通過傳遞none作爲默認返回的第二個參數。 –

0

如果你打算使用發電機的兩倍,未做查詢兩次,你可以使用itertools.tee有發電機的兩個副本供你使用:

from itertools import tee 

res1, res2 = tee(res, 2) 

# consume the first generator: 
for pod in res1: 
    ... 
    ... 

# you have a second generator you can use: 
print(next(res2.results).text) 
... 
... 
0

source code表明res.podsres.results份額相同的迭代器。你所得到的錯誤僅僅意味着有沒有結果。嘗試不同的查詢。

樣本查詢工作,例如:

>>> res = client.query('temperature in Washington, DC on October 3, 2012') 
>>> print(next(res.results).text) 
(21 to 27) °C (average: 24 °C) 
(Wednesday, October 3, 2012) 
>>> [p.title for p in res] 
['Input interpretation', 'Result', 'History', 'Weather station information'] 

您具體查詢不返回任何結果,顯然是因爲有假設確認;訪問http://api.wolframalpha.com/v2/query?input=length+of+a+Ferrari+458&appid=<your-app-id>生產:

<?xml version='1.0' encoding='UTF-8'?> 
<queryresult success='false' 
    error='false' 
    numpods='0' 
    datatypes='' 
    timedout='' 
    timedoutpods='' 
    timing='2.742' 
    parsetiming='0.79' 
    parsetimedout='false' 
    recalculate='' 
    id='MSPa12051ddfeh1dgh883d2e000035eh08fba72b042e' 
    host='http://www4f.wolframalpha.com' 
    server='9' 
    related='' 
    version='2.6' 
    profile='EnterDoQuery:0.,StartWrap:2.74235'> 
<didyoumeans count='2'> 
    <didyoumean score='0.365929' level='medium'>Ferrari 458</didyoumean> 
    <didyoumean score='0.26087' level='low'>length</didyoumean> 
</didyoumeans> 
</queryresult> 

如何,你可以從那裏得到的Web界面管理,以提取2015 Ferrari 458 Italia | overall length查詢不是從API文檔清晰。

可以訪問經由Result.tree屬性didyoumean元件,使用ElementTree API

>>> res = client.query('length of a Ferrari 458') 
>>> for didyoumean in res.tree.findall('//didyoumean'): 
...  print didyoumean.text 
... 
Ferrari 458 
length 
+0

感謝Martijin,有用但仍然不給我回答我的查詢。有沒有可能的方式使用Wolfram API來解答它? –

+0

我還沒有找到方法。 API文檔也不提供任何提示。 –