4

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.

1
  • OK, so where is your code ? and whats the problem with it ? Commented Dec 15, 2014 at 13:00

3 Answers 3

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'
Sign up to request clarification or add additional context in comments.

1 Comment

I'll accept it , although I prefer using string functions over regex if possible.
3
>>> 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

Comments

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

1 Comment

Python doesn't have !, you can use not instead

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.