gpt4 book ai didi

python - 我如何将其写入文件?我已经实现了 Twitter 流 API (Python)...但它只是打印到控制台

转载 作者:行者123 更新时间:2023-12-01 06:14:30 25 4
gpt4 key购买 nike

from getpass import getpass
from textwrap import TextWrapper
import tweepy
import time

class StreamWatcherListener(tweepy.StreamListener):
status_wrapper = TextWrapper(width=60, initial_indent=' ', subsequent_indent=' ')
def on_status(self, status):
try:
print self.status_wrapper.fill(status.text)
print '\n %s %s via %s\n' % (status.author.screen_name, status.created_at, status.source)
except:
# Catch any unicode errors while printing to console
# and just ignore them to avoid breaking application.
pass
def on_error(self, status_code):
print 'An error has occured! Status code = %s' % status_code
return True # keep stream alive
def on_timeout(self):
print 'Snoozing Zzzzzz'



username="abc"
password="abc"
auth = tweepy.BasicAuthHandler(username, password)
listener = StreamWatcherListener()
stream=tweepy.Stream(auth,listener)
stream.filter(locations=[-122.75,36.8,-121.75,37.8,-74,40,-73,41])

这只是打印到控制台。但如果我想用它做更多事情怎么办?我正在使用的库是 here .

最佳答案

您正在使用打印语句。

打开一个文件并将要打印到控制台的字符串写入。

在你的代码中

class StreamWatcherListener(tweepy.StreamListener):
status_wrapper = TextWrapper(width=60, initial_indent=' ', subsequent_indent=' ')

def __init__(self, api=None):
self.file = open("myNewFile")
super(StreamWatcherListener, self).__init__(api)

....

关于python - 我如何将其写入文件?我已经实现了 Twitter 流 API (Python)...但它只是打印到控制台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4050184/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com