Python Forum

Full Version: read write
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
def some_condition(line):
    return "ddd" in line

with open('testin.csv') as infile:
    for line in infile:
        if some_condition(line):
            donotline=line
        else:
            f = open('testout.csv','a+')
            f.write(line)
I have been trying to read from testin and write to testout.csv with the above code. The code runs but take approimatley 5 mins to read through 160000 lines. Is there any means to speed it up.
thanks
nuncio
Don't open a new output file every time through the loop. Just open it once before the loop starts.
Try this perhaps
import itertools as itt

def some_condition(line):
    return "ddd" in line

open('testout.csv', 'w').writelines(itt.filterfalse(condition, open('testin.csv')))