0

How do i get the screen names from a list of twitter IDs? I have the IDs saved in a pandas dataframe and have 38194 IDs that i wish to match to their screen names so i can do a network analysis. I am using python, but i am quite new to coding so i do not know if this is even possible? I have tried the following:

myIds = friend_list

if myIds:

myIds = myIds.replace(' ','')
myIds = myIds.split(',')
# Set a new list object
myHandleList = []
i = 0
# Loop trough the list of usernames
for idnumber in myIds:
    u = api.get_user(myIds[i])
    uid = u.screen_name
    myHandleList.append(uid)
    i = i+1
# Print the lists
print('Twitter-Ids',myIds)
print('Usernames',myHandleList)
    #set a filename based on current time
csvfilename = "csvoutput-"+time.strftime("%Y%m%d%-H%M%S")+".csv"
print('We also outputted a CSV-file named '+csvfilename+' to your file parent directory')
with open(csvfilename, 'w') as myfile:
    wr = csv.writer(myfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
    wr.writerow(['username','twitter-id'])
    j = 0
    for handle in myHandleList:
        writeline = myHandleList[j],myIds[j]
        wr.writerow(writeline)
        j = j+1

else: print('The input was empty')

  • Welcome to Stack Overflow! Please provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) - a small subset of your data as a copyable piece of code that can be used for testing as well as your expected output. For more information, see [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391). You can [edit](https://stackoverflow.com/posts/67677876/edit) your post. – AlexK May 24 '21 at 19:54

1 Answers1

0

Updating your loop, as I believe you are pretty close.

myHandleList = []
myIds = ['1031291359', '960442381']
for idnumber in myIds:
    u = api.get_user(idnumber)
    myHandleList.append(u.screen_name)

print(myHandleList)
Jonathan Leon
  • 5,440
  • 2
  • 6
  • 14