Aartika Sethi Aartika Sethi - 2 years ago 124
Python Question

Selectively delete rows in a csv file (Python)

I am using a txt file with unwanted words.i have a csv file wit 4 columns ,i need to find if unwanted word is present in any of columns i want to delete that full row.

ex-

unwanted_list = ['one', 'on']


input csv1

name class label test

ne two 1 five,

on one 2 we.

as we 20 on

cast as none vote

output csv 1

name class label test

cast as none vote

Answer Source

A simple script using the python set object should do the trick. This checks that there are no words common to both the set of unwanted words, and the set of words in the line of the input file;

def filter_unwanted_words():
    unwanted_words = {'one', 'on'}
    with open('input.csv', 'r') as f:
        for line in f:
            if set(line.split()).isdisjoint(unwanted_words):
                yield line


def write_output():
    with open('output.csv', 'w') as f:
        f.writelines((line for line in filter_unwanted_words()))

if __name__ == '__main__':
    write_output()

The output in output.csv for this is;

ne two 1 five,
cast as none vote
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download