3

I have a multiline text string, that looks like:

AAAA BBBBBB
BBBB VVVV XXXX

CCCCCCCC XXXX

I'd like to come up with a small function that removes an entire line if it contains a word/phrase , so that for above if I lets say sent in 'VVV' as a paremeter the output would be:

AAAA BBBBBB

CCCCCCCC XXXX

There are many examples on stackoverflow, eg Remove lines that contain certain string, which show how to do this for a file, but I'm not sure how without opening a file.

Community
  • 1
  • 1
user1592380
  • 34,265
  • 92
  • 284
  • 515

3 Answers3

9

you can use re.sub:

>>> import re
>>> my_string
'AAAA BBBBBB\nBBBB VVVV XXXX\n\nCCCCCCCC XXXX'
>>> re.sub(".*VVV.*\n?","",my_string)
'AAAA BBBBBB\n\nCCCCCCCC XXXX'

you can define a function and can do for any substring:

>>> def remove(rem,my_string):
...     return re.sub(".*"+rem+".*\n?","",my_string)
... 
>>> remove("VVV",my_string)
'AAAA BBBBBB\n\nCCCCCCCC XXXX'
>>> remove("XXX",my_string)
'AAAA BBBBBB\n\n'
>>> remove("BBB",my_string)
'\nCCCCCCCC XXXX'
>>> remove("CCCC",my_string)
'AAAA BBBBBB\nBBBB VVVV XXXX\n\n'
Hackaholic
  • 19,069
  • 5
  • 54
  • 72
2
>>> text = '''AAAA BBBBBB
BBBB VVVV XXXX

CCCCCCCC XXXX'''
>>> text = '\n'.join(line for line in text.split('\n') if 'VVV' not in line)
>>> print text
AAAA BBBBBB

CCCCCCCC XXXX
jamylak
  • 128,818
  • 30
  • 231
  • 230
1
inp = "AAAA BBBBBB\nBBBB VVVV XXXX\n\nCCCCCCCC XXXX"

ans = ""
pattern = "VVVV"
for line in inp.split("\n"):

    if line.find(pattern)<0:
        ans=ans + line+"\n"
print ans
ZdaR
  • 22,343
  • 7
  • 66
  • 87