在python中使用请求执行异常时出错

时间:2014-03-08 22:13:13

标签: python json exception-handling python-requests

我一直在寻找我的问题背后的原因无济于事。它在我添加异常处理程序之前工作,所以我相对困惑。我是python,JSON,API和sql的新手,但不是编程。

def getSyllables(word):
conn = sqlite3.connect("wordbase.db")
c = conn.cursor()
c.execute('SELECT * FROM words WHERE Word =?', [word])
syllables = 0
if (c.fetchone() == None):
    url = 'http://rhymebrain.com/talk?function=getWordInfo&word=' + word
    try:
        r = requests.get(url)
        j = json.loads(r.text)
        rememberSyllables(word, int(j['syllables'])
        syllables = int(j['syllables'])
    except requests.exceptions.RequestException as e:
        syllables = 0
else:
    syllables = c.fetchone[1]
return syllables

我现在得到的错误是:

syllables = int(j['syllables'])
        ^
SyntaxError: invalid syntax

如果我删除它(即使我很确定语法是正确的吗?):

except requests.exceptions.RequestException:
     ^
SyntaxError: invalid syntax

`

2 个答案:

答案 0 :(得分:1)

问题很简单,你还没有关闭前一行的括号:

rememberSyllables(word, int(j['syllables']))

这应该可以解决问题。

答案 1 :(得分:1)

rememberSyllables(word, int(j['syllables']))这里额外的)会解决问题!!