python – AttributeError: list object has no attribute replace when trying to remove character

python – AttributeError: list object has no attribute replace when trying to remove character

xpath method returns a list, you need to iterate items.

kickoff = [item.replace(, ) for item in kickoff]
kickoff = tree.xpath(//*[@id=page]/div[1]/div/main/div/article/div/div[1]/section[2]/p[1]/b[1]/text())

This code is returning list not a string.Replace function will not work on list.

[i.replace(, ) for i in kickoff ]

python – AttributeError: list object has no attribute replace when trying to remove character

This worked for me:

kickoff = str(tree.xpath(//*[@id=page]/div[1]/div/main/div/article/div/div[1]/section[2]/p[1]/b[1]/text()))
kickoff = kickoff.replace(, )

This error is caused because the xpath returns in a list. Lists dont have the replace attribute. So by putting str before it, you convert it to a string which the code can handle. I hope this helped!

Leave a Reply

Your email address will not be published.