in Python, trying to do this thing: I have a line contains several strings separated by ','. 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:
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 )
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
. 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) + "."
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.