Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am trying to remove the character ' from my string by doing the following

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

This gives me the error AttributeError: 'list' object has no attribute 'replace'

Coming from a php background I am unsure what the correct way to do this is?

like image 391
emma perkins Avatar asked Apr 15 '16 09:04

emma perkins


2 Answers

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

kickoff = [item.replace("'", "") for item in kickoff]
like image 51
falsetru Avatar answered Sep 25 '22 21:09

falsetru


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 ]
like image 25
Himanshu dua Avatar answered Sep 24 '22 21:09

Himanshu dua