Home > front end >  How to write nested loop and avoid that it overwrites data?
How to write nested loop and avoid that it overwrites data?

Time:07-11

I don't get it

Hi all, I'm trying to get the recent tweets from multiple users. There for i created a nested Loop, but my problem is that it overwrites "berlintweets". So the Loop works, but it should add the data instead of overwriting it.

Really any help or advice would be highly appreciated. I'm a total beginner, who's proud that he made it this far.. but now I'm sadly stuck and don't really know what i can do / change anymore.

Thank you all in advance!

import tweepy
import config
import pandas as pd

client = tweepy.Client(bearer_token=config.BEARER_TOKEN)
Liste_Namen = pd.read_csv('Namen.csv', delimiter=',')
tweet_id_list = [""]
tweet_text_list = [""]
tweet_created_list = [""]
berlintweet = [""]

for user_name in Liste_Namen['ids']:
    berlintweet = tweepy.Paginator(client.get_users_tweets, id=user_name, exclude= ["replies", "retweets"], tweet_fields=["created_at"], max_results=100).flatten(limit=200)

    for tweet in berlintweet:
        tweet_id_list.append(tweet.id)
        tweet_text_list.append(tweet)
        tweet_created_list.append(tweet.created_at)

df = pd.DataFrame({
'name': config.USER_ID,
'tweet_id': tweet_id_list,
'tweet_text': tweet_text_list,
'tweet_created': tweet_created_list,

})
df.to_csv('BerlinTest2.csv', sep=',', index=False, encoding='utf-8-sig')

CodePudding user response:

It should be possible to append to berlintweet, just as you append to your other lists, such as tweet_id_list:

    page = tweepy.Paginator(
        client.get_users_tweets, 
        id=user_name, 
        exclude=["replies", "retweets"], 
        tweet_fields=["created_at"], 
        max_results=100)    
    berlintweet.append(page.flatten(limit=200))

For more information, the following might be useful to you: https://docs.python.org/3/library/stdtypes.html#mutable-sequence-types

It did not immediately become apparent to me what flatten() returns, but the name suggest to me that it's a list (https://docs.tweepy.org/en/latest/v2_pagination.html). However, you can also pass a generator to append(). It should modify the list it's called on in-place.

Also, you initialize tweet_id_list with a string and then attempt to access .id of each element, which will not work. A string does not have .id. The same issue applies to berlintweet = [""]. You want to initialize it with berlintweet = []. This is either a copy-paste error or you are still unclear how to initialize a variable with an empty list. An empty list is [], while [""] means a list with an empty string as only element. Actually, it might apply to all four of your lists, as I suspect there is no good reason to have an empty string in there, unless it is some requirements external to this portion of the code.

When you append the tweet texts to tweet_text_list, you probably want to write tweet.text. I believe that's what the documentation states, but I do not have the ability to test out the code right now, as I do not have a Twitter account.

For other readers of this answer, the documentation of the available fields for a tweet are here:

https://developer.twitter.com/en/docs/twitter-api/fields

https://docs.tweepy.org/en/latest/v2_models.html#tweet

  • Related