[英]a line contains strings,separated by ','
in Python, trying to do this thing: I have a line contains several strings separated by ','. 在Python中,尝试执行以下操作:我有一行包含由','分隔的几个字符串。 I want to erase every string that has particular substring in it.
我想删除其中具有特定子字符串的每个字符串。 for example:
例如:
line: first_name,second_name,every_single_name_exicted,dog,cat.
I want to erase every string that has the word "name" in it + the ',' it has, so the result would be: 我想删除其中包含单词“ name” +“'”的每个字符串,因此结果将是:
line: dog,cat.
and not: 并不是:
line: ,,,dog,cat
what can I use to achieve that? 我可以用什么来实现这一目标? and how generally can I search particular substring when I don't care what it has on its left or right (like the search option in NotePad for example when you can write name )
以及当我不在乎某个子字符串的左侧或右侧时,如何一般搜索该子字符串(例如,当您可以输入name时,例如NotePad中的搜索选项)
s = 'first_name,second_name,every_single_name_exicted,dog,cat'
s = s.split(',')
a = ','.join([i for i in s if 'name' not in i])
print(a)
Try this 尝试这个
if not in
. if not in
按“名称”过滤。 Like this: 像这样:
mystring = "first_name, second_name, every_single_name_exicted,dog,cat"
newstring = ", ".join(s.strip() for s in mystring.split(",") if not "name" in s) + "."
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.