2

I need to replace "!@#$%^&*()\n{}[]()_-+=<>?\xa0;'/.," with a blank. I am using replace method but it seems it is deprecated on python 3.6. word_list = [] is a list which will have all the words extracted from the webpage. Then clean_up_list method will clean the symbols and replace them with blank space. I used for to loop through the length of symbols and replace symbols with blank. I used word = word.replace(symbols[i],"") ; Any help on how to use the replace method so that symbols are replaced and words are printed without symbols between them.

Error:

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

My Code:

url = urllib.request.urlopen("https://www.servicenow.com/solutions-by-category.html").read()
word_list = []
soup = bs.BeautifulSoup(url,'lxml')
word_list.append([element.get_text() for element in soup.select('a')])
print(word_list)

def clean_up_list(word_list):
    clean_word_list = []
    for word in word_list:
        symbols = "!@#$%^&*()\n{}[]()_-+=<>?\xa0;'/.,"
        for i in range(0,len(symbols)):

            word  = word.replace(symbols[i],"")
            #print(type(word))

                #print(type(word))
                #word.replace(symbols[i]," ")
        if(len(word) > 0):
            #print(word)
            clean_word_list.append(word)
self.Fool
  • 302
  • 2
  • 14
  • 1
    Possible duplicate of [Best way to strip punctuation from a string in Python](https://stackoverflow.com/questions/265960/best-way-to-strip-punctuation-from-a-string-in-python) – Chris_Rands Jun 22 '17 at 14:31
  • 2
    `replace` is not depreciated, it is a string method, not for lists. See the suggested dupe for how to perform a string replacement – Chris_Rands Jun 22 '17 at 14:32

2 Answers2

0

There are two errors here: first you do not construct a list of strings, but a list of lists of strings. This line:

word_list.append([element.get_text() for element in soup.select('a')])

should be:

word_list.extend([element.get_text() for element in soup.select('a')])

Furthermore you cannot call replace on the list directly (it is not a method of a list object). You need to this for every entry.

Next you also specify (correctly) than you then have to call replace(..) for every character in the symbols string. Which is of course inefficient. You can however use translate(..) for that.

So you can replace the entire for loop with with list comprehension:

symbols = "!@#$%^&*()\n{}[]()_-+=<>?\xa0;'/.,"
clean_word_list = [word.translate(None,symbols) for word in word_list]
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
-1

Try explicitly converting the word to a string, as the error code you're receiving mentions the object is a 'list' not string and that the replace method cannot be called on lists. For example (notice the second to last line):

def clean_up_list(word_list):
clean_word_list = []
for word in word_list:
    word = str(word)
    symbols = "!@#$%^&*()\n{}[]()_-+=<>?\xa0;'/.,"
Graham Streich
  • 874
  • 3
  • 15
  • 31