从python中的字符串列表中删除字符

问题描述 投票:-1回答:2

我有一个包含url和其他一些单词的字符串列表。 URL看起来像这样:<a href="https://www.blabla/k20832">我想知道是否有办法告诉python删除<>中的所有内容。

我用它来删除其他单词:

list = [w.replace('hello', '') for w in list]
python string list condition erase
2个回答
0
投票

你可以使用正则表达式。

import re
str = "<a href=\"https://www.blabla/k20832\"> word1 word2"
str2 = re.sub('<a.*?>', ' ', str)

0
投票

您可以使用查找功能。

str = '< a href="https://www.blabla/k20832 > word1 word2'

start_Index=str.find('<')

end_Index=str.find('>')

print(str[:start_Index+1]+str[end_Index:]) 
© www.soinside.com 2019 - 2024. All rights reserved.