Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.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 我如何让我的程序将它生成的所有数据写入一个文本文件,我正在尝试对Twitter提要进行情感分析?_Python_Twitter_Io_Text Files_Bigdata - Fatal编程技术网

Python 我如何让我的程序将它生成的所有数据写入一个文本文件,我正在尝试对Twitter提要进行情感分析?

Python 我如何让我的程序将它生成的所有数据写入一个文本文件,我正在尝试对Twitter提要进行情感分析?,python,twitter,io,text-files,bigdata,Python,Twitter,Io,Text Files,Bigdata,好的,所以我试着对推特推特进行情感分析,我所有的代码都能完美地获得实时推特的回复。但是,shell会在达到一定数量后删除所有tweet。我一直在乱搞我的代码,试图把所有的tweet都写进一个文本文件,但在我挣扎的最后5个小时里,我无法理解它。其中注释符号是我添加的代码,用于尝试将信息写入文本文件。我是python新手,如果有人能帮我,我将非常感激 我会使用Git,因为我知道如何在该程序中将所有数据写入文本文件,但我不知道如何让它运行python文件 def twitterreq(url, met

好的,所以我试着对推特推特进行情感分析,我所有的代码都能完美地获得实时推特的回复。但是,shell会在达到一定数量后删除所有tweet。我一直在乱搞我的代码,试图把所有的tweet都写进一个文本文件,但在我挣扎的最后5个小时里,我无法理解它。其中注释符号是我添加的代码,用于尝试将信息写入文本文件。我是python新手,如果有人能帮我,我将非常感激

我会使用Git,因为我知道如何在该程序中将所有数据写入文本文件,但我不知道如何让它运行python文件

def twitterreq(url, method, parameters):
  req = oauth.Request.from_consumer_and_token(oauth_consumer,
                                         token=oauth_token,
                                         http_method=http_method,
                                         http_url=url, 
                                         parameters=parameters)

  req.sign_request(signature_method_hmac_sha1, oauth_consumer, oauth_token)

  headers = req.to_header()

  if http_method == "POST":
    encoded_post_data = req.to_postdata()
  else:
    encoded_post_data = None
    url = req.to_url()

  opener = urllib.OpenerDirector()
  opener.add_handler(http_handler)
  opener.add_handler(https_handler)

  response = opener.open(url, encoded_post_data)

  return response

def fetchsamples():
  url = "https://stream.twitter.com/1/statuses/sample.json"
  parameters = []
  response = twitterreq(url, "GET", parameters)
  f=open("C:\\Users\\name\\Desktop\\datasci_course_materials\\assignment1", "w") # my attempt
  for line in response:
    f.write(str(line) + "\n") # 100% sure im not using this command properly
    print line.strip()

if __name__ == '__main__':
  fetchsamples()

我省略了代码的顶部,因为我们不需要我的访问和消费者密钥来回答这个问题。这段代码是Python2.7中的

可以尝试以下内容

try:
    with open("filename.txt", "a") as f:
        for n in response:
            f.write(n + "\n")
        f.close()
except IOError as e:
    print e
except TypeError as t:
    print t
这将尝试打开filename.txt并将每个项目追加到新行中。它将捕获IO错误和类型错误。

行f=open,w my trunt表示如果停止,您的文件将完全丢失并被擦除。每次程序运行这一行时,它都会删除文件,然后将其打开

尝试更改模式a,这意味着每次后续调用都会在末尾添加数据

f = open("<filename>", "a") # Appending instead of overwriting.

额外信息:

你说shell删除推文是什么意思?脚本是否结束,然后重新启动脚本,原始tweet就消失了?我必须让它运行10分钟,以捕获足够的大数据,以便对其进行分析,python在运行过程中的某个点后开始删除这些数据。因此,我需要捕获大约10分钟的所有数据,以便对其进行分析。是的,shell在我重新启动之前删除了它,但它保留了大约30秒到1分钟的Tweets。当我尝试实现您建议的代码时,它抛出了这个错误,在我尝试之前,它是相同的错误。文件C:\Users\jake\Desktop\datasci\u course\u materials\assignment1\twitterstream.py,第73行,在fetchsamples文件C:\Users\jake\Desktop\datasci\u course\u materials\assignment1\twitterstream.py的第58行中,在fetchsamples f=openC:\\Users\\jake\\Desktop\\datasci\u course\u materials\\assignment1中,w IOError:[Errno 13]权限被拒绝:“C:\\Users\\jake\\Desktop\\datasci\u course\u materials\\assignment1”回溯显示assignment1是一个文件夹,而不是像代码中暗示的那样是一个文件,请在其末尾添加一个文件名。您试图将文件夹用作无法工作的文件,错误就是这样说的。