【问题标题】:Getting Syntax Error where none was before出现以前没有的语法错误
【发布时间】:2014-08-09 18:32:42
【问题描述】:

我有这个代码sn-p:

import sqlite3
import cld
import langid

languagedata = [] #list for the returned data from database
n = -1
connector = sqlite3.connect("GERMANY.db")
selecter = connector.cursor()
selecter.execute(''' SELECT title FROM DATAGERMANY''')
for row in selecter: #iterate through all the rows in db
    print (row)
    lan = langid.classify("{}".format(row)) #identify the language
    print (lan)
    connector.execute('''update DATAGERMANY set title_abbreviation_langid=? , title_reliability_langid=? where id_db == ? ''',(lan[-2], lan[-1])
 connector.commit() #save changes
 connector.close()

我运行了 100 次,效果很好。我添加了另一个for loop,然后再次将其删除。所以我不应该更改代码中的任何内容。但现在我收到 connector.commit()Invalid Syntax 错误。

这怎么可能?!

注意:没有关于错误的更多信息。

【问题讨论】:

  • 回溯会很有用
  • @PadraicCunningham 什么是回溯? idl 高亮connector red
  • 您收到的完整错误代码
  • @PadraicCunningham Invalid Syntax 然后单词 connector 被突出显示为红色。不多了
  • 所以当你运行代码时你没有得到正确的回溯输出?

标签: python sqlite error-handling syntax-error


【解决方案1】:

您在这一行缺少一个结束括号:

connector.execute('''update DATAGERMANY set title_abbreviation_langid=? , title_reliability_langid=? where id_db == ? ''',(lan[-2], lan[-1])) # added paren here

我添加了一个结束括号,你的代码现在应该没问题了。

【讨论】:

  • 不敢相信!太感谢了!奇怪的是它转到connector 虽然
  • 感谢您的陪伴!
  • 不用担心,我偷偷怀疑这是问题所在,但我在屏幕截图中看不到您的代码,通常错误可能出现在 python 在回溯中显示错误之前的行中。
  • 这一定是我删除嵌套循环时发生的。不知道。我盯着connector 并试图找出它出了什么问题。我从上面复制了 10 次,但仍然是错误。很高兴知道 python 会时不时地超出实际错误的一行。
猜你喜欢
  • 2015-10-13
  • 2020-07-05
  • 2023-03-15
  • 1970-01-01
  • 2014-11-17
  • 1970-01-01
  • 2019-03-09
  • 2021-02-21
相关资源
最近更新 更多