How to remove lines of a textfile with a boolean as a rule in python?

Question:

I need to remove lines of a .txt file that start with certain values, say anything with a digit 0-9 or an !.

I have already written a function noNumsExclam that looks something like this:

def noNumsExclam(s: str) -> bool:
    return bool(re.search("regexpattern", s))

It returns True if the string contains what matches the pattern and False if it does not. I have confirmed this is working on my code.

Now I want to use the function noNumsExclam to remove the lines that return "True" and store the text with deleted lines in a new variable.

I think an easier solution would be to use re.sub() however, I have been given this specific constraint for the problem.

Asked By: Abigail Swallow

||

Answers:

Slap a filter on your list of lines.

lines = ['0a', 'b', '!c']
filtered = [*filter(lambda l: not noNumsExclam(l), lines)]

What this does is tell Python to keep the lines which noNumsExclam returns falsy values for, and throws away lines which are truthy.

filter returns an iterator, * unpacks everything into the list outside.

Alternatively:

filtered = [l for l in lines if not noNumsExclam(l)]
Answered By: TrebledJ
text = '''
Line to be kept1
1010 Line to be removed
Line to be kept2
!Anothter line to be removed'''

def noNumsExclam(st, pattern):
    return not re.match(rf'{pattern}',st)

new_List  = []

for l in text.splitlines():
    if noNumsExclam(l, r'd|!'):
        new_List.append(l)
# join the filtered lines into text, ignoring the empty elements via filter(None,...
updated_text = 'n'.join(filter(None,new_List))

updated_text

Line to be kept1
Line to be kept2
Answered By: LetzerWille
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.