Skip to main content
 首页 » 编程设计

python之根据 python 中的某些列表删除特定字符串

2025年05月04日18zengkefu

<分区>

我有字符串,来自一些 xml 元素:

source = "<![CDATA[<p><span stylefontfamily times new romantimesserif fontsize large>Is important ?</span></p>]] 

”

我想根据这个列表删除指定的字符串:

mustDelWord=["<p>","</p>","<span stylefontfamily times new romantimesserif fontsize large>","</span>"]

所以,预期的输出是:

<![CDATA[Is important ?]]> 

到目前为止代码是:

mustDelWord=["<p>","</p>","<span stylefontfamily times new romantimesserif fontsize large>","</span>"]   
 
for i in mustDelWord: 
        source = source.replace(mustDelWord[i],"") 
print source 

但是出现这个错误:

source = source.replace(mustDelWord[i],"") 

TypeError: 列表索引必须是整数,而不是 str

谢谢。