【问题标题】:Python API Streaming, write new file after certain sizePython API Streaming,在一定大小后写入新文件
【发布时间】:2017-08-29 23:12:11
【问题描述】:

我有一个 python 脚本,它维护与 Twitter 流 API 的开放连接,并将数据写入 json 文件。在当前正在写入的文件达到一定大小后,是否可以在不中断连接的情况下写入新文件?例如,我刚刚流式传输数据超过 1 周,但所有数据都包含在单个文件 (~2gb) 中,因此解析速度很慢。如果我可以在之后写入一个新文件,比如 500mb,那么我将有 4 个较小的文件(例如,dump1.json、dump2.json 等)来解析而不是一个大文件。

import tweepy
from tweepy import OAuthHandler
from tweepy import Stream
from tweepy.streaming import StreamListener

# Add consumer/access tokens for Twitter API
consumer_key = '-----'
consumer_secret = '-----'
access_token = '-----'
access_secret = '-----'

auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)

api = tweepy.API(auth)

# Define streamlistener class to open a connection to Twitter and begin consuming data
class MyListener(StreamListener):
def on_data(self, data):
    try:
        with open('G:\xxxx\Raw_tweets.json', 'a') as f:
            f.write(data)
            return True
    except BaseException as e:
        print("Error on_data: %s" % str(e))
        return True
def on_error(self, status):
   print(status)
   return True

bounding_box = [-77.2157,38.2036,-76.5215,39.3365]#filtering by location
keyword_list = ['']#filtering by keyword

twitter_stream = Stream(auth, MyListener())
twitter_stream.filter(locations=bounding_box) # Filter Tweets in stream by location bounding box
#twitter_stream.filter(track=keyword_list) # Filter Tweets in stream by keyword

【问题讨论】:

    标签: python twitter twitter-streaming-api


    【解决方案1】:

    由于您每次都重新打开文件,这相当简单 - 在文件名中使用索引并在文件大小达到阈值时推进它

    class MyListener(StreamListener):
        def __init(self):
            self._file_index = 0
    
        def on_data(self, data):
            tweets_file = 'G:\xxxx\Raw_tweets{}.json'.format(self._file_index)
            while os.path.exists(tweets_file) and os.stat(tweet_file).st_size > 2**10:
                self._file_index += 1 
                tweets_file = 'G:\xxxx\Raw_tweets{}.json'.format(self._file_index)
    ....
    

    该循环将负责重启您的应用

    【讨论】:

    • os.stat(tweet_file).st_size > 2**10是怎么设置文件大小的?
    • @AndrewR.,这是您检查文件大小的方式。我首先检查存在以避免异常 - 您可以使用 try...except。您可以将此代码打包到 getter 方法中 - 适合您的风格
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多