user3731831
user3731831

Reputation: 13

Save multiple lines of text in .txt

I am a python newbie. I can print the twitter search results, but when I save to .txt, I only get one result. How do I add all the results to my .txt file?

t = Twython(app_key=api_key, app_secret=api_secret, oauth_token=acces_token, oauth_token_secret=ak_secret)

tweets                          =   []
MAX_ATTEMPTS                    =   10
COUNT_OF_TWEETS_TO_BE_FETCHED   =   500

for i in range(0,MAX_ATTEMPTS):

    if(COUNT_OF_TWEETS_TO_BE_FETCHED < len(tweets)):
    break

    if(0 == i):
    results = t.search(q="@twitter",count='100')

    else:
    results = t.search(q="@twitter",include_entities='true',max_id=next_max_id)


    for result in results['statuses']:
    tweet_text = result['user']['screen_name'], result['user']['followers_count'],     result['text'], result['created_at'], result['source']
    tweets.append(tweet_text)
    print tweet_text

    text_file = open("Output.txt", "w")
    text_file.write("@%s,%s,%s,%s,%s" % (result['user']['screen_name'], result['user']['followers_count'], result['text'], result['created_at'], result['source']))
    text_file.close()

Upvotes: 1

Views: 215

Answers (2)

Blckknght
Blckknght

Reputation: 104682

There are two general solutions to your issue. Which is best may depend on more details of your program.

The simplest solution is just to open the file once at the top of your program (before the loop) and then keep reusing the same file object over and over in the later code. Only when the whole loop is done should the file be closed.

with open("Output.txt", "w") as text_file:
    for i in range(0,MAX_ATTEMPTS):
        # ...

        for result in results['statuses']:
            # ...

            text_file.write("@%s,%s,%s,%s,%s" % (result['user']['screen_name'],
                                                 result['user']['followers_count'],
                                                 result['text'],
                                                 result['created_at'],
                                                 result['source']))

Another solution would be to open the file several times, but to use the "a" append mode when you do so. Append mode does not truncate the file like "w" write mode does, and it seeks to the end automatically, so you don't overwrite the file's existing contents. This approach would be most appropriate if you were writing to several different files. If you're just writing to the one, I'd stick with the first solution.

for i in range(0,MAX_ATTEMPTS):
    # ...

    for result in results['statuses']:
        # ...

        with open("Output.txt", "a") as text_file:
            text_file.write("@%s,%s,%s,%s,%s" % (result['user']['screen_name'],
                                                 result['user']['followers_count'],
                                                 result['text'],
                                                 result['created_at'],
                                                 result['source']))

One last point: It looks like you're writing out comma separated data. You may want to use the csv module, rather than writing your file manually. It can take care of things like quoting or escaping any commas that appear in the data for you.

Upvotes: 1

Mike Driscoll
Mike Driscoll

Reputation: 33071

You just need to rearrange your code to open the file BEFORE you do the loop:

t = Twython(app_key=api_key, app_secret=api_secret, oauth_token=acces_token, oauth_token_secret=ak_secret)

tweets                          =   []
MAX_ATTEMPTS                    =   10
COUNT_OF_TWEETS_TO_BE_FETCHED   =   500

with open("Output.txt", "w") as text_file:
    for i in range(0,MAX_ATTEMPTS):

        if(COUNT_OF_TWEETS_TO_BE_FETCHED < len(tweets)):
        break

        if(0 == i):
        results = t.search(q="@twitter",count='100')

        else:
        results = t.search(q="@twitter",include_entities='true',max_id=next_max_id)


        for result in results['statuses']:
        tweet_text = result['user']['screen_name'], result['user']['followers_count'],     result['text'], result['created_at'], result['source']
        tweets.append(tweet_text)
        print tweet_text

        text_file.write("@%s,%s,%s,%s,%s" % (result['user']['screen_name'], result['user']['followers_count'], result['text'], result['created_at'], result['source']))
        text_file.write('\n') 

I use Python's with statement here to open a context manager. The context manager will handle closing the file when you drop out of the loop. I also added another write command that writes out a carriage return so that each line of data would be on its own line.

You could also open the file in append mode ('a' instead of 'w'), which would allow you to remove the 2nd write command.

Upvotes: 1

Related Questions