Python 使用 RE 在 for 文本文件中查找整数

Python using RE to find integer in text file in a for

我正在 python 中使用 python 2.7 的 tweepy 编写一个机器人。我对如何处理我想要做的事情感到困惑。目前,机器人会找到推文 ID 并将其附加到文本文件中。在以后的运行中,我想使用正则表达式在该文件中搜索匹配项,并且仅在文本文件中没有匹配项时才写入。目的不是将重复的推文 ID 添加到我的文本文件中,这可能会跨越大量数字后跟换行符。

感谢任何帮助!

/edit 当我尝试下面的代码时,IDE 说看不到匹配项,结果出现语法错误。

import re,codecs,tweepy
qName = Queue.txt
tweets = api.search(q=searchQuery,count=tweet_count,result_type="recent")
with codecs.open(qName,'a',encoding='utf-8') as f:
    for tweet in tweets:
        tweetId = tweet.id_str
        match = re.findall(tweedId), qName)
        #if match = false then do write, else discard and move on
        f.write(tweetId + '\n')

如果我没听错,你不必费心正则表达式等。让特殊容器完成工作 you.I 将继续使用非重复容器,如 dictionaryset 例如,将文件中的所有数据读入 dictionaryset,然后将 id 扩展到此 dictionaryset 中,最后写入此 dictionaryset 返回文件。

例如

>>>data = set()
>>>for i in list('asddddddddddddfgggggg'):
    data.add(i)

>>>data
>>>set(['a', 's', 'd', 'g', 'f']) ## see one d and g