'Replace part of a string in a for loop
I want to iterate through the list followers_list to edit each item.
followers_list = []
for follower in tweepy.Cursor(api.get_followers, screen_name='@'+screen_name, wait_on_rate_limit=True, count=200).items(9000):
followers_list.append([follower.profile_image_url_https])
for i in followers_list:
print(i.replace('normal', '400x400'))
Each item in the list is a URL like https://pbs.twimg.com/profile_images/1xxxxxxx/KLJHG-1367_normal.jpg that I want to change as https://pbs.twimg.com/profile_images/1xxxxxxx/KLJHG-1367_400x400.jpg
I'm returned the error :
Traceback (most recent call last):
File "C:\Data\Mes documents\twitter\twitter_dl_pictures.py", line 30, in <module>
url = [i.replace('normal', '400x400') for i in followers_list]
File "C:\Data\Mes documents\twitter\twitter_dl_pictures.py", line 30, in <listcomp>
url = [i.replace('normal', '400x400') for i in followers_list]
AttributeError: 'list' object has no attribute 'replace'
Solution 1:[1]
Use that instead:
for follower in tweepy.Cursor(api.get_followers, screen_name='@'+screen_name, wait_on_rate_limit=True, count=200).items(9000):
followers_list.append(follower.profile_image_url_https)
Explanation: Instead of adding the string to the list, you are adding a list containing the string.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Cubix48 |
