Home > OS >  How to use Python to write to a CSV File?
How to use Python to write to a CSV File?

Time:05-20

I am trying to use Python and Tweepy to scrape a list of followers from a specific user. I can get the program to print the list of users but I am having trouble getting it to write to a CSV file. How would I make it so that it writes to a CSV file? Also, how would I make it so that there are no commas in between individual users screen names in the CSV file?

import tweepy
import csv

consumer_key = "XXX"
consumer_secret = "XXX"
access_token = "XXX"
access_token_secret = "XXX"

auth = tweepy.OAuth1UserHandler(
  consumer_key, consumer_secret,
   access_token, access_token_secret
)

api = tweepy.API(auth, wait_on_rate_limit=True)

csvFile = open('followers.csv', 'a')
csvWriter = csv.writer(csvFile)

for follower in api.get_followers(screen_name='twitteruser'):

  print(follower.screen_name)
  
  csvWriter.writerow(follower.screen_name)
csvFile.close()

CodePudding user response:

Since you are already working with external dependencies (such as Tweepy) I suggest you working with Pandas! It's the best package to handle CSVs - however you don't want to use it everywhere to do every simple task because it's very heavy, but I don't think it will matter in you case.

You can also read and write the CSV with other delimiters, such as tabs or whatever character you want, using the argument sep in the pd.to_csv method, solving your comma demand.

CodePudding user response:

Also, I'd recommend opening your file for writing:

csvFile = open('followers.csv', 'w')

CodePudding user response:

You don't have to use the csv package here, as your use-case is pretty simple. Also, I'd highly recommend using a context manager instead of manually opening and closing files.

Here's a way of doing what you want:

with open('followers.csv', 'a') as csvFile:
    for follower in api.get_followers(screen_name='twitteruser'):
        csvFile.write(f"{follower.screen_name}\n")
  • Related