Home > Back-end >  AttributeError: 'list' object has no attribute 'replace' Python
AttributeError: 'list' object has no attribute 'replace' Python

Time:10-08

What am I missing in the below? I keep getting error message:

AttributeError: 'list' object has no attribute 'replace'

I've followed the advice here and elsewhere but something is still not working.

import json
import requests

search_terms = input('Name of university: ')
parameters = {"affiliation": search_terms}
response = requests.get("https://api.ror.org/organizations", params=parameters)

ror_payload = response.json()

name_variations_provisional = []
for organisation in ror_payload['items']:
    if organisation['chosen']==True:
        name_variations_provisional.append(organisation['organization']['name'])
        name_variations_provisional.append(organisation['organization']['aliases'])
        for relationship in organisation['organization']['relationships']:
            if relationship['type'] == 'Child':
               name_variations_provisional.append(relationship['label'])

#this is the line that's not working
name_variations=[str.replace("C", "") for str in name_variations_provisional]

print(name_variations)

CodePudding user response:

organisation['organization']['aliases'] is probably a list. Use extend() rather than append() so that each element is appended separately, rather than the whole list being appended as a single element.

name_variations_provisional.extend(organisation['organization']['aliases'])
  • Related