在For循环之后清除Python中的元组
我正在努力弄清楚如何在从数据库读取后清除元组“数据” 工艺流程:在For循环之后清除Python中的元组,python,for-loop,tuples,mysql-python,Python,For Loop,Tuples,Mysql Python,我正在努力弄清楚如何在从数据库读取后清除元组“数据” 工艺流程: 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 ((109
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()
每隔X分钟,我就会调用batchUpdate
batchUpdate拉入符合特定条件的记录
我们迭代这些记录以执行更新
进程结束并等待下一个调用
问题:
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()
对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()
代码:
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()
综上所述,这两条语句应该覆盖先前数据变量中的任何内容
我要指出的是,这个函数似乎不需要全局变量——您可以而且可能应该使用局部变量
无论如何,我不认为问题在于存在一些神秘的遗留数据,除非定义了b.fetchall()
对象来实现这一点。(例如,如果查询中有错误,或者查询没有返回匹配项,它是如何进行通信的?如果它引发或返回一个您忽略的值,则可能fetchall
可能会返回过时的数据,因为您应该检查该值,而不是调用fetchall…)
我建议您看看execute
和fetchall
是如何协同工作的,还可以看看for循环。我看到b.execute
和u.execute
和update.commit
。看起来您有许多不同的数据库连接。也许你喜欢。或者你复制/粘贴代码,你真的应该这样做:
u.execute(...)
u.commit()
我可能是误会了。您能在函数末尾简单地设置data=None
吗?但是b.execute
返回不同的数据吗?有什么理由将数据定义为全局的吗?我对数据库连接/游标很笨。问题解决了。谢谢