简体   繁体   English

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

[英]remove characters from a list of strings in python

I have a list of strings that contain a url plus some other words.我有一个包含 url 和其他一些单词的字符串列表。 The URL looks like this: <a href="https://www.blabla/k20832"> I was wondering if there is a way to tell python to delete everything inside the <> . URL 如下所示: <a href="https://www.blabla/k20832">我想知道是否有办法告诉 python 删除<>所有内容。

I was using this to delete other words:我用它来删除其他词:

list = [w.replace('hello', '') for w in list]

You can use regex.您可以使用正则表达式。

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

You can use find function.您可以使用查找功能。

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:]) 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM