我正在努力弄清楚如何在從數據庫中讀取數據後清除元組'數據'。在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'返回不同的數據?是否有任何理由將數據定義爲全局? –