python mysqldb表更新不工作
我是一个PythonNoob,在我对维德做了一些情绪分析之后,我在更新表的MySQLdb操作方面遇到了麻烦。脚本如下所示:python mysqldb表更新不工作,python,mysql,Python,Mysql,我是一个PythonNoob,在我对维德做了一些情绪分析之后,我在更新表的MySQLdb操作方面遇到了麻烦。脚本如下所示: #!/usr/bin/env python from nltk.tokenize import sent_tokenize from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer import MySQLdb as my try: db = my.connect(host="*",
#!/usr/bin/env python
from nltk.tokenize import sent_tokenize
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
import MySQLdb as my
try:
db = my.connect(host="*",
user="*",
passwd="*",
db="*")
cur = db.cursor()
cur.execute("SELECT id,content FROM text where relscore >=100")
for row in cur.fetchall():
tableid = row[0]
textin = row[1].rstrip('\n') + ' '
sentences = sent_tokenize(textin)
analyzer = SentimentIntensityAnalyzer()
sum = 0
count = 0
for sentence in sentences:
vs = analyzer.polarity_scores(sentence)
count += 1
sum += vs['compound']
avg = sum / count
cur.execute("update text set sentiment = %s where id = %s",(avg,tableid))
db.close()
except my.Error as e:
print(e)
当我在没有try/except的情况下运行这个时,它将按预期运行,不会出现错误,但是在数据库中没有更新值。我添加了try/except来尝试访问来自mysql的任何错误,但是我得到了一个语法错误
except my.Error as e:
^
IndentationError: unindent does not match any outer indentation level
我不明白这一点,因为它似乎与try的缩进级别相匹配,而且它看起来像我找到的示例。但就像我说的,我是个笨蛋,所以我可能错过了一些明显的东西
我已经用调试器验证了select语句正在工作,avg正在计算,并且已经验证了用户对db和表具有必要的权限。有人可以帮忙吗?您必须在执行查询后提交更改(在关闭光标之前):
至于
try…except
block:在except
word之前有额外的空间。您必须在执行查询之后(关闭光标之前)提交更改:
至于
try…except
block:在except
word之前有多余的空格。除了my之外,前面还有空格。错误为e:
拆下它并重新测试
#!/usr/bin/env python
from nltk.tokenize import sent_tokenize
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
import MySQLdb as my
try:
db = my.connect(host="*", user="*", passwd="*", db="*")
cur = db.cursor()
cur.execute("SELECT `id`, `content` FROM text WHERE `relscore`>=100")
for row in cur.fetchall():
tableid = row[0]
textin = row[1].rstrip('\n') + ' '
sentences = sent_tokenize(textin)
analyzer = SentimentIntensityAnalyzer()
sum = 0
count = 0
for sentence in sentences:
vs = analyzer.polarity_scores(sentence)
count += 1
sum += vs['compound']
avg = sum / float(count)
cur.execute("UPDATE text SET `sentiment` = %s WHERE `id` = %s", (avg,tableid))
db.commit()
cur.close()
db.close()
except Exception as e:
print(e)
您之前有一个空格,但我的错误为e: 拆下它并重新测试
#!/usr/bin/env python
from nltk.tokenize import sent_tokenize
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
import MySQLdb as my
try:
db = my.connect(host="*", user="*", passwd="*", db="*")
cur = db.cursor()
cur.execute("SELECT `id`, `content` FROM text WHERE `relscore`>=100")
for row in cur.fetchall():
tableid = row[0]
textin = row[1].rstrip('\n') + ' '
sentences = sent_tokenize(textin)
analyzer = SentimentIntensityAnalyzer()
sum = 0
count = 0
for sentence in sentences:
vs = analyzer.polarity_scores(sentence)
count += 1
sum += vs['compound']
avg = sum / float(count)
cur.execute("UPDATE text SET `sentiment` = %s WHERE `id` = %s", (avg,tableid))
db.commit()
cur.close()
db.close()
except Exception as e:
print(e)
好啊显然,它必须是db.commit。这就成功了。谢谢,好的。显然,它必须是db.commit。这就成功了。谢谢。前面有一个空格,除了我的。错误为e:。删除并再次测试。除my之外,前面有一个空格。错误为e:。移除并再次测试它