0

I have a Python script that compares two files, checks whether or not there have been any changes made and stores any changes to a list. I am trying to implement another list of words, characters, and other things for the script to ignore as changes. This list will be stored in a file called ignore.txt.

My first thought was when a something from the ignore list is seen in the list of changes, to remove it from the list and continue. Below is my solution, but it is not going to work since the length in the for loop changes and it won't look at each change. I've ran into a road block and need some assistance.

for i in changes:
    for j in ignore:
        if j in i:
            changes.remove(i)
            break
        else:
            print("Do something else")
wovano
  • 4,543
  • 5
  • 22
  • 49
Jrm1715
  • 3
  • 3
  • Could you add a small example of input files and expected output to make it more clear what you want the script to do? – wovano May 05 '20 at 11:08

1 Answers1

0

You really want to use comprehension here:

filtered_changes = (line for line in changes if not any(word in line for word in ignore))

You can then iterate over filtered_changes, or convert it to a list and do whatever you want with it.

You can replace the call to any with a regular expression call, like this:

import re
anyword = re.compile("|".join(ignore))
filtered_changes = (line for line in changes if not anyword.search(line))

This is assuming the words in ignore do not contain regular expression special characters. As long as they are all letters and numbers, you should be safe. IF not, you can escape the words as discussed at Escaping regex string in Python.

Amitai Irron
  • 1,973
  • 1
  • 12
  • 14