首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Python For循环中清除元组

在Python For循环中清除元组
EN

Stack Overflow用户
提问于 2017-02-10 16:27:06
回答 1查看 454关注 0票数 1

在从数据库中读取数据之后,我很难弄清楚如何清除元组“数据”。

流程:

每隔X分钟,我就打电话给batchUpdate

batchUpdate提取符合特定条件的记录

我们遍历那些执行更新的记录。

进程结束并等待下一次调用。

发行:

每次对函数batchUpdate的后续调用都不会产生新的数据。元组'data‘包含与以前相同的值。

简化的示例(每1秒只提取一条记录):

代码语言:javascript
复制
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'),)

代码:

代码语言:javascript
复制
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()       
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-10 16:43:38

在您的代码中,您将执行以下两行:

代码语言:javascript
复制
 global data

 data = b.fetchall()

综合起来,这两个语句应该覆盖以前data变量中的任何内容。

我要指出的是,这个函数似乎不需要全局变量--您可以而且很可能应该为此使用一个局部变量。

无论如何,我不认为问题是存在一些神秘的剩余数据,除非定义了b.fetchall()对象。(例如,如果查询中有错误,或者查询没有返回匹配项,那么它是如何传递的?如果它引发或返回一个您忽略的值,也许fetchall会返回陈旧的数据,因为您应该检查该值,而不是调用fetchall.)

我建议您看看executefetchall是如何一起工作的,还可以看看您的for循环。我看到了b.executeu.executeupdate.commit。似乎您有许多不同的数据库连接。也许你知道。或者,您可以复制/粘贴代码,您确实应该这样做:

代码语言:javascript
复制
u.execute(...)
u.commit()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42164147

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档