【发布时间】:2017-02-10 16:27:06
【问题描述】:
我正在努力弄清楚如何在从数据库读取后清除元组“数据”。
流程:
每 X 分钟,我调用一次 batchUpdate
batchUpdate 拉入符合特定条件的记录
我们遍历那些执行更新的记录
流程结束,等待下一次调用
问题:
随后对函数 batchUpdate 的每次调用都不会产生新数据。元组“数据”包含与以前相同的值。
简化示例(仅拉取一条记录,每 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吗? -
但是
b.execute返回不同的数据?是否有任何理由将数据定义为全局数据?
标签: python for-loop tuples mysql-python