替换for循环中字符串的一部分



我想遍历列表followers_list来编辑每个项目。

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'))

列表中的每个项目都是一个类似https://pbs.twimg.com/profile_images/1xxxxxxx/KLJHG-1367_normal.jpg的URL,我想将其更改为https://pbs.twimg.com/profile_images/1xxxxxxx/KLJHG-1367_400x400.jpg

我返回错误:

Traceback (most recent call last):
File "C:DataMes documentstwittertwitter_dl_pictures.py", line 30, in <module>
url = [i.replace('normal', '400x400') for i in followers_list]
File "C:DataMes documentstwittertwitter_dl_pictures.py", line 30, in <listcomp>
url = [i.replace('normal', '400x400') for i in followers_list]
AttributeError: 'list' object has no attribute 'replace'

使用它:

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)

解释:不是将字符串添加到列表中,而是添加一个包含该字符串的列表。

最新更新