在 For 循环后清除 Python 中的元组
Clear Tuple in Python after For Loop
我正在努力弄清楚如何在从数据库读取后清除元组 'data'。
处理流程:
每隔 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()
在您的代码中,您正在执行这两行:
global data
data = b.fetchall()
合起来,这两个语句应该覆盖之前 data
变量中的任何内容。
我要指出,此函数似乎不需要 全局变量 - 您可以,而且可能应该为此使用局部变量。
无论如何,我不认为问题是存在一些神秘的剩余数据,除非定义了 b.fetchall()
对象来做到这一点。 (例如,如果您的查询中有错误,或者您的查询 returns 没有匹配项,它是如何传达的?如果它引发或 returns 一个您忽略的值,也许 fetchall
可能会返回过时的数据,因为您应该检查该值而不是调用 fetchall...)
我建议你看看 execute
和 fetchall
是如何协同工作的,还要看看你的 for 循环。我看到 b.execute
和 u.execute
以及 update.commit
。看起来您有许多不同的数据库连接。也许你会。或者,也许你 copy/pasted 代码,你真的应该做这样的事情:
u.execute(...)
u.commit()
我正在努力弄清楚如何在从数据库读取后清除元组 'data'。
处理流程:
每隔 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()
在您的代码中,您正在执行这两行:
global data
data = b.fetchall()
合起来,这两个语句应该覆盖之前 data
变量中的任何内容。
我要指出,此函数似乎不需要 全局变量 - 您可以,而且可能应该为此使用局部变量。
无论如何,我不认为问题是存在一些神秘的剩余数据,除非定义了 b.fetchall()
对象来做到这一点。 (例如,如果您的查询中有错误,或者您的查询 returns 没有匹配项,它是如何传达的?如果它引发或 returns 一个您忽略的值,也许 fetchall
可能会返回过时的数据,因为您应该检查该值而不是调用 fetchall...)
我建议你看看 execute
和 fetchall
是如何协同工作的,还要看看你的 for 循环。我看到 b.execute
和 u.execute
以及 update.commit
。看起来您有许多不同的数据库连接。也许你会。或者,也许你 copy/pasted 代码,你真的应该做这样的事情:
u.execute(...)
u.commit()