-2

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.

ars
  • 64
  • 6

2 Answers2

1
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
LetzerWille
  • 5,355
  • 4
  • 23
  • 26
  • This is really helpful. One thing I'm struggling with is in the for loop I don't want to print the result, I want to add it to text. But I'm not sure how to do so. Setting l = noNumsExclam doesn't work but I'm not sure what else to try. – ars Sep 03 '22 at 21:34
  • 1
    @AbigailSwallow, you can initialize a new list like newList = [], and then instead of printing the lines, append the lines to newList. I will update the answer right now. – LetzerWille Sep 03 '22 at 21:39
  • 1
    @AbigailSwallow, I forgot to join the filtered list, to get the new text. Fixed. – LetzerWille Sep 03 '22 at 22:29
0

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)]
TrebledJ
  • 8,713
  • 7
  • 26
  • 48