【问题标题】:How do I create a file that doesn't contain information older than 5 minutes?如何创建不包含超过 5 分钟信息的文件?
【发布时间】:2019-01-26 06:05:47
【问题描述】:

我正在开发 Twitter Streaming api 以获取实时推文数据。

我可以将该数据打印到控制台。但我想要的是将数据保存到一个文件中,并且该数据不应超过 5 分钟。

我如何连续滚动保存过去 5 分钟数据的文件,就像我们对日志文件所做的那样。

同时该文件应该可供阅读。

有没有办法在 Python 中做到这一点?

我还没有遇到过这样的事情,我们可以提到文件可以保存特定数据的持续时间。

【问题讨论】:

  • 创建一个包含所有要保留在活动窗口中的条目的数据结构,并定期将该数据结构刷新到同一个文件,并在每次刷新时覆盖文件的内容。
  • 只是出于好奇,我不确定您要达到什么目的。看起来您想将该文件用作两个进程之间的通信?正如您所提到的,您想要同时读/写?如果没有,可能有更好的方法。
  • 我正在分析过去 5 分钟内的流数据。我想我需要将它保存在某个地方并从那里读取,以便每分钟从推文中创建一份报告。

标签: python file text-files tweepy twitter-streaming-api


【解决方案1】:

搜索“python logrotate”会发现:https://docs.python.org/3.7/library/logging.handlers.html#logging.handlers.TimedRotatingFileHandler。 可能值得一试。

如果您更愿意自己编写代码,管理内存中的推文(例如使用collections.deque,这样很容易弹出旧的并附加新的),然后不时将其刷新到文件中...... , (或使用套接字、pickle,只需将变量名称传递给您的分析函数,正如其他答案中已经提到的)。

【讨论】:

    【解决方案2】:

    这应该可以帮助您入门。只要推文的年龄超过 300 秒,它就会从列表中删除推文。

    import time;
    TIME_LIMIT = 300; # 5 mins in seconds
    
    
    # Get the tweet text and the time stamp
    def GetTweet():
        #...
        # Wait until there is a new tweet from Donald :)
        text = 'your function that gets the text goes here';
    
        # Get the time stamp
        time_ = time.time();
        return text, time_
    
    
    # Kill all the old data that is older than X seconds
    def KillOldTweet(tweets):
        time_now = time.time();  # Capture the current time
    
        # For every tweet stored remove the old one
        for i, tweet in enumerate(tweets):
            time_diff = time_now - tweet[1]; # get the time difference
            if time_diff > TIME_LIMIT: # if older than X secods, kill that tweet from the list
                tweets.pop(i);
                pass;      
            pass;
    
        return tweets; # return the list
    
    # Updates the file with the list of text of tweets
    def UpdateFile(tweets):
        with open('output.txt', 'w') as file:  # open the file and close it after writing
            texts = [ i for i, j in tweets ]; # unzip;
            out_text = str(texts); # convert to string
            print(out_text); #
            file.write(out_text); # overwrite to file
            pass;
        pass;
    
    
    tweets = [];
    while(1):
        # Get the new tweet and append to the list
        text, time_ = GetTweet(); # Wait until a new tweet arrived
        tweet = (text, time_); # zip it into a tuple
        tweets.append(tweet); # append it to list
    
    
        # Kill the old tweets from the list
        tweets = KillOldTweet(tweets)
    
        # Update the file with the fresh tweets
        UpdateFile(tweets);
    
        # Sleep for 1 second.
        time.sleep(1);
    
        pass;
    

    但我建议您使用套接字模块而不是写入文本文件。或者使用 pickle 模块轻松解压文件中的内容

    【讨论】:

      【解决方案3】:

      将数据与实际时间保存在一个文件中,并检查实际时间是否相差 5 分钟。利用时间。或者使用睡眠功能,每 5 分钟清除一次旧数据。

      【讨论】:

        猜你喜欢
        • 2014-01-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-22
        • 2016-10-27
        • 1970-01-01
        相关资源
        最近更新 更多