Comparing two csv files in Python
Question: I have this program that takes two csv files into consideration. It looks at
"testclaims" (one column many rows) and sees if any words in "masterlist"(one
column, many rows) are within the rows of "testclaims." If the rows in
"testclaims" contains any word in "masterlist" it will list it into a new .csv
file called "output." This part of the program works great.

The part that I can't seem to figure out is to output all the remaining rows
in "testclaims" that don't contain ANY words in "masterlist" into another csv
called "output2" I would think that the last two lines of my code should get
this to work, but it's not outputting what I want. I hope I've explained this
clearly enough. Here's my code:

    
    
        import csv
    
        with open("testclaims.csv") as file1, open("masterlist.csv") as file2,
            open("stopwords.csv") as file3,\
            open("output.csv", "wb+") as file4, open("output2.csv", "wb+") as file5:
            writer = csv.writer(file4)
            writer2 = csv.writer(file5)
            key_words = [word.strip() for word in file2.readlines()]
            stop_words = [word.strip() for word in file3.readlines()]
            internal_stop_words = [' a ', ' an ', ' and ', 'as ', ' at ', ' be ', 'ed ',
                  'ers ', ' for ',\
                  ' he ', ' if ', ' in ', ' is ', ' it ', ' of ', ' on ', ' to ', 'her ', 'hers '\
                  ' do ', ' did ', ' a ', ' b ', ' c ', ' d ', ' e ', ' f ', ' g ', ' h ', ' i ',\
                  ' j ', ' k ', ' l ', ' m ', 'n ', ' n', ' nc ' ' o ', ' p ', ' q ', ' r ', ' s ',\
                  ' t ', ' u ', ' v ', ' w ', ' x ', ' y ', 'z ', ',', '"', 'ers ', ' th ', ' gc ',\
                           ' so ', ' ot ', ' ft ', ' ow ', ' ir ', ' ho ', ' er ', ]
            for row in file1:
                row = row.strip()
                row = row.lower()
                for stop in stop_words:
                    if stop in row:
                        row = row.replace(stop," ")
                for stopword in internal_stop_words:
                    if stopword in row:
                        row = row.replace(stopword," ")
                for key in key_words:
                    if key in row:
                        writer.writerow([key, row])
                    elif key not in row:
                        writer2.writerow([row])
    

What output2 is outputting is every row in "testclaims" repeated multiple
times.

For example if "testclaims" contains this one column:

    
    
        Happy
        Sad
        Angry
        Dog
        Cat
    

"output2" is outputting a csv that prints this one column:

    
    
        Happy
        Happy
        Happy
        Happy
        Happy
        Sad
        Sad
        Sad
        Sad
        Angry
        Angry
        Angry
        Angry
        Angry
        Dog
        Dog
        Dog
        Dog
        Dog
        Cat
        Cat
        Cat
        Cat
        Cat
    

And it doesn't output the same number of each row either.


Answer: you have a double for loop and each time you print the row, but you only want
it at most once per row. you should adjust your last two lines:

    
    
    for row in file1:
    
        ...
    
        for key in key_words:
            if key in row:
                writer.writerow([key, row])
        if not any(key in row for key in key_words):
            writer2.writerow([row])
    

