Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 检查txt文件中是否包含ID_Python_Python 3.x - Fatal编程技术网

Python 检查txt文件中是否包含ID

Python 检查txt文件中是否包含ID,python,python-3.x,Python,Python 3.x,我想从一个特定的用户那里下载新的推文,并使用一些其他规则进行过滤。如何交叉引用我正在处理的tweet中的tweet ID和tweetid.txt文件中的ID,以避免重复我在NRE_tweet文件中保存的内容 这就是我到目前为止所写的,正在产生副本的内容 i = 0 for tweet in NRE_tweets: tweet_ids = open('tweetid.txt', 'a+') if NRE_tweets[i]['in_reply_to_screen_name']

我想从一个特定的用户那里下载新的推文,并使用一些其他规则进行过滤。如何交叉引用我正在处理的tweet中的tweet ID和tweetid.txt文件中的ID,以避免重复我在NRE_tweet文件中保存的内容

这就是我到目前为止所写的,正在产生副本的内容

i = 0
for tweet in NRE_tweets:

    tweet_ids = open('tweetid.txt', 'a+')

    if NRE_tweets[i]['in_reply_to_screen_name'] is None:

        if NRE_tweets[i]['id_str'] not in tweet_ids.readlines():
            print("adding tweet " + str(NRE_tweets[i]['id_str']))
            info_wanted.append(NRE_tweets[i]['text'])
            info_wanted.append(NRE_tweets[i]['id_str'])
            info_wanted.append(NRE_tweets[i]['created_at'])

            NRE_file = open('NRE.txt', 'a')
            NRE_file.write(str(info_wanted) + '\n')
            NRE_file.close()

            append_tweet_ids = open('tweetid.txt', 'a')
            append_tweet_ids.write(NRE_tweets[i]['id_str'] + '\n')
            append_tweet_ids.close()

    tweet_ids.close()
    info_wanted = []

    i += 1
编辑:感谢您的建议,工作代码现在已排序。我可以做一些事情让它更干净,但现在。。。它起作用了

NRE_tweets = t.statuses.user_timeline(screen_name='NRE_northern')
i = 0

NRE_file = open('NRE.txt', 'a')
openFile = shelve.open('tweetid')

try:
    loadIDs = openFile['list_id']
    print("list_id's loaded")
except:
    print("exception entered")
    loadIDs = []

for tweet in NRE_tweets:
    if NRE_tweets[i]['in_reply_to_screen_name'] is None: # check that tweet isn't a reply
        if NRE_tweets[i]['id_str'] in loadIDs:
            print(str(NRE_tweets[i]['id_str']) + ' already stored')

        else:
            print("adding " + str(NRE_tweets[i]['id_str']))
            # added wanted elements to a list
            info_wanted.append(NRE_tweets[i]['text'])
            info_wanted.append(NRE_tweets[i]['id_str'])
            info_wanted.append(NRE_tweets[i]['created_at'])

            # added list to txt file
            NRE_file.write(str(info_wanted) + '\n')

            loadIDs.append(NRE_tweets[i]['id_str'])
            openFile['list_id'] = loadIDs

    info_wanted = []

    i += 1

print(openFile['list_id'])  
NRE_file.close()
openFile.close()

如果x是None:,不要在代码中使用
,除非
x
有可能是字面上的
None
。因为只有
None是None
而其他所有人(0、空iterables等)都是伪造者:),所以如果不是x,则应该使用

readlines()
返回文件中的行,包括每行的行尾
\n
。因此,如果(NRE_tweets[i]['id_str']+'\n')不在tweet_id.readlines()中,您应该编写
if(NRE_tweets[i]['id_str']+'\n'):

就像在注释中建议的那样,在for循环之前打开文件一次,在for循环之后关闭。还可以考虑使用<代码>搁置>代码>模块(或代码> SqLITE3);这将使处理数据变得容易得多

编辑:

我还注意到您打开了两次
tweetid.txt
,中间没有关闭。IF块内不需要第二个
open()
。您只需使用第一个文件句柄调用
write()
,即可将新ID添加到文件中。您还应该在循环外部调用
readlines()
,并将其保存到一个列表中,然后在for循环头中使用该列表,因为使用新的代码结构,对
readlines()
的后续调用将在文件耗尽时返回一个空字符串。因此,当您找到一个新ID时,您可以将其附加到此列表中,并调用
write()
将该ID添加到
tweetid.txt

另一种方法是,首先以读取模式打开文件,调用
readlines()
并将结果保存到列表中,然后关闭文件。启动循环并执行列表上的所有操作;添加新ID,删除,等等。在循环结束时,以写入模式重新打开
tweetid.txt
,并将列表内容写入文件;它将覆盖旧内容。如果要添加大量新ID,请使用此方法


对代码进行结构化,以便只打开文件一次,然后对其进行操作,最后将其关闭。

不要一直打开文件,请在循环外打开一次,如果不想重复,则必须从文件中加载数据。我认为一个文件可能使用了错误的结构,但是,当数据从twitter下载时,你可能至少想要转储一个dict或者类似sqlite数据库的东西,如果tweet是对某个人的回复,那么该键为:“in_reply_to_screen_name”,它或者返回一个回复对象的字符串。如果帖子不是回复,则为None值。我只想要这些并没有价值的推文。我已经重新编写了代码以包括书架。在我使用家用电脑之前,我无法测试它,因为我无法将模块安装到工作电脑上。但我在空闲时做了一些测试。概念似乎起作用。谢谢多亏了一些帮助。在我的目标中还有一段路要走。但我有一个工作代码,至少可以得到我想要的数据。编辑以添加工作代码。@Levo Nice!不过有几件事:
openFile['list_id']=loadIDs
可以放在循环之后,一次性地进行更改。另外,由于您知道
info\u wanted
始终只有3个项目,因此您可以在循环之前使用
info\u wanted=[“”]*3
预先分配空间,然后在循环中覆盖这些位置的项目,而不是调用
append()
,这从长远来看成本更高。不过,这些都是次要的优化,只有在处理大量数据时才有必要。另外,由于tweetid.txt只是一个ID列表,因此
pickle
模块可能更合适。干杯:)