我正在努力弄清楚如何在从数据库中读取数据后清除元组'数据'。在For循环中清除Python中的元组
的工艺流程:
每隔X分钟,我打电话BATCHUPDATE
BATCHUPDATE在符合某种条件的记录拉
我们通过这些记录进行更新
过程结束迭代并等待下一个电话
问题:
随后每次调用batchUpdate函数都不会产生新数据。元组'data'包含与之前相同的值。
简化的例子(只拉一个记录,计划每隔1秒):
Update has started
((10938L, u"@anonymized @anonymized House of Cards will be nothing compared to the Drumpf's!And worst of all is that Americans chose him as president?", u'New York, USA'),)
Update has started
((10938L, u"@anonymized @anonymized House of Cards will be nothing compared to the Drumpf's!And worst of all is that Americans chose him as president?", u'New York, USA'),)
Update has started
((10938L, u"@anonymized @anonymized House of Cards will be nothing compared to the Drumpf's!And worst of all is that Americans chose him as president?", u'New York, USA'),)
Update has started
((10938L, u"@anonymized @anonymized House of Cards will be nothing compared to the Drumpf's!And worst of all is that Americans chose him as president?", u'New York, USA'),)
代码:
class analyzeRecords():
def batchUpdate(self):
global data
#Select up to 1 record
b.execute(""" SELECT id, tweet,tweet_location_guess FROM rawTweets where compoundScore IS NULL LIMIT 0,1 """)
#Put that data into a tuple
data = b.fetchall()
print(data)
#print that update has started
print("Update has started")
for row in data:
idMatch = row[0]
cleanTweet = reduce(lambda x, y: x.replace(y, d[y]), sorted(d, key=lambda i: len(i), reverse=True), row[1].lower())
sentimentScores = analyzer.polarity_scores(cleanTweet)
overallScore = sentimentScores["compound"]
u.execute("""UPDATE rawTweets SET tweet=%s, compoundScore=%s where id=%s""",
(cleanTweet, overallScore, idMatch))
update.commit()
l = task.LoopingCall(analyzeRecords().batchUpdate)
l.start(timeout) # call every sixty seconds
reactor.run()
我可能会误解。你可以简单地在函数的结尾处设置'data = None'吗? – Fagan
但是'b.execute'返回不同的数据?是否有任何理由将数据定义为全局? –