简体   繁体   English

AttributeError:'list'对象在尝试删除字符时没有属性'replace'

[英]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' 这给了我错误AttributeError:'list'对象没有属性'replace'

Coming from a php background I am unsure what the correct way to do this is? 来自php背景我不确定这样做的正确方法是什么?

xpath方法返回一个列表,需要迭代项。

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

此代码返回列表而不是string.Replace函数将无法在列表上工作。

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

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. 导致此错误的原因是xpath在列表中返回。 Lists don't have the replace attribute. 列表没有replace属性。 So by putting str before it, you convert it to a string which the code can handle. 因此,通过在它之前放置str,可以将其转换为代码可以处理的字符串。 I hope this helped! 我希望这有帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError:“ list”对象在尝试删除“ / n”时没有属性“ replace” - AttributeError: 'list' object has no attribute 'replace' while trying to remove '/n' AttributeError: 'NoneType' object 在尝试替换时没有属性 'replace' - AttributeError: 'NoneType' object has no attribute 'replace' when trying to replace AttributeError:'list'对象没有属性'replace' - AttributeError: 'list' object has no attribute 'replace' AttributeError: 'list' object 没有属性 'replace' Python - AttributeError: 'list' object has no attribute 'replace' Python AttributeError: 'list' 对象没有属性 'replace' "fasttext" - AttributeError: 'list' object has no attribute 'replace' "fasttext" AttributeError:“列表”对象没有属性“替换”, - AttributeError: 'list' object has no attribute 'replace', “AttributeError:'list' object 没有属性'replace' - "AttributeError: 'list' object has no attribute 'replace' 如何修复AttributeError:当我尝试替换csv读取中的某些文本时,“列表”对象没有属性“替换” - How to fix AttributeError: 'list' object has no attribute 'replace' when I try to replace some text in a csv read 试图做一个联系人列表 AttributeError: 'list' object has no attribute 'write' - trying to do a contact list AttributeError: 'list' object has no attribute 'write' python AttributeError:“ NoneType”对象在列表中没有属性“ replace” - python AttributeError: 'NoneType' object has no attribute 'replace' in list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM