skip to Main Content

I am getting data from Twitter with Tweepy and using Stream API… Here is my t_stream.py file:

access_token = "my token"
access_token_secret = "my t_secret"
consumer_key = "my key"
consumer_secret = "my c_secret"

class StdOutListener(StreamListener):
    def on_data(self, raw_data):
        data = json.loads(raw_data)
        print(json.dumps(data, ensure_ascii=False))

    def on_error(self, status):
        print (status)

if __name__ == '__main__':

    dinle = StdOutListener()
    auth = OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    stream = Stream(auth, dinle)

I have a file called veri.txt when i use code:

    python3 t_stream.py > veri.txt

Here is a part of result data: https://paste.pound-python.org/show/SiUalejehr6T8BEvYOpw/

From this veri.txt file, i want to get information like:

retweet_count:

followers_count:

name:

text:

and i want to store these coloumns as an output csv file…
I tried some codes but it didnt work. So i am asking a python code for reading a text file line by line, getting the information which i mentioned above from the line and storing them as coloumn in data file as a csv file. Can someone help me about this subject? Thank you.

2

Answers


  1. some example from the python3 docs:

    import csv
    with open('eggs.csv', 'w', newline='') as csvfile:
        spamwriter = csv.writer(csvfile, delimiter=' ',
                                quotechar='|', quoting=csv.QUOTE_MINIMAL)
        spamwriter.writerow(['Spam'] * 5 + ['Baked Beans'])
        spamwriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])
    
    Login or Signup to reply.
  2. Not a python example, but you can do this with jq filters pretty easy piping the output of the stream.

    $ oksocial https://stream.twitter.com/1.1/statuses/sample.json | jq -r 'select(.id?) | [.user.name, .retweet_count, .user.followers_count, .text] | @csv'
    
    
    "˗ˏˋ molly ˎˊ˗",0,176,"RT @Real_Liam_Payne: What a hero"
    "DerrickMadelyn",0,62,"Benefits upon hiring a calculator herein tampa differently rapt: xsLysp"
    "LemanLogan",0,67,"Do the improve on hood provision rates into the friendly desideratum.: FxzCOjcA"
    "larron prayer-uh♑️",0,696,"Last summer was definitely the best one I've had"
    "Joel Garza",0,102,"@ewa_jodlowska @dougnap 
    Gah! We were going to ask about those yesterday. The PyCon Mexico organizers wanted to take one."
    "BrianEmma",0,44,"However on prize lone in relation with the immeasurably fructuous electronic flageolet: fsnFMQR"
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search