Michal K
Michal K

Reputation: 255

Python extract certain rows

I have a csv file named test.csv . It contains certain rows with string for instance "Blue". How can I create a new csv file without the rows containing the string "Blue".
N.B Came to this as the find and replace method doesn't remedy the situation.

Upvotes: 0

Views: 559

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174696

You may use not in

with open(file) as f, open(outfile, 'w') as w:
    for line in f:
        if 'Blue' not in line:
            w.write(line)

or

with open('infile') as f, open('outfile') as w:
    for line in f:
        for i in line.split(','):
            if 'Blue' in i:
                break
        else:
            w.write(line)    

Upvotes: 1

Related Questions