尝试在Python中写入.npy文件,但遇到一些问题

时间:2019-09-26 16:19:08

标签: python numpy csv pycharm

使用Twitter API,我正在提取数据并将其保存到文件中。 I'm trying to convert this code到一个将数据保存到.npy文件而不是.csv文件的文件中。这段代码可以正常工作,但是,当将其转换为将其保存为.npy的代码时,我遇到了一些问题。以下是我所做的修改,以便将其保存到NPY文件中。

import tweepy
import numpy as np

#input your credentials here
consumer_key = 'xx'
consumer_secret = 'xx'
access_token = 'xx'
access_token_secret = 'xx'

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth,wait_on_rate_limit=True)

for tweet in tweepy.Cursor(api.search,
                           q="#stressed",
                           count=100,
                           include_rts=False,
                           tweet_mode='extended',
                           lang="en",
                           since="2019-09-15").items(100):
    if (not tweet.retweeted) and ('RT @' not in tweet.full_text) and ('https://' not in tweet.full_text):
        print (tweet.full_text)
        np.save('test', tweet.full_text.encode('utf-8'))

当我打开创建的文件时,它仅显示为已保存一行(因此,我猜测np.save每次都会覆盖它)。我知道我可以提取Twitter数据,因为在我的PyCharm控制台上,我可以看到所有100条推文。如何将其保存在我的.npy文件中,以便将每个tweet存储为新行?

我看到np.savetxt可以用,但是我在设置语法时遇到了麻烦。当我将np.save更改为np.savetxt时,它说“预期的1D或2D数组,取而代之的是0D数组。如果有人可以引导我完成解决此问题的最佳方法,那将是令人惊讶的。我是python的新手和numpy库,所以我希望有更多经验的人可以指导我。

0 个答案:

没有答案
相关问题