我有一个字符串,来自某个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
谢谢。你知道吗
只需将行更改为:
这是因为循环
for i in mustDelWord
已经在列表中的元素上迭代,而不是该列表中的索引,所以您不必执行mustDelWord[i]
。你知道吗相关问题 更多 >
编程相关推荐