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

Time:09-03

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.

CodePudding user response:

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)]

CodePudding user response:

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)

for l in text.splitlines():
    if noNumsExclam(l, r'\d|!'):
        print(l)

Line to be kept1
Line to be kept2
  • Related