Search code examples
pythonfilecsvreadline

How can I read part of file and write the rest to another file?


I have multiple large csv file. How can I read part of each file and write 10% of the data/rows to another file?


Solution

  • This works for me:

    with open("in.csv") as infile, open("out.csv", "w") as outfile:
        outcsv = csv.writer(outfile)
        for i, row in enumerate(csv.reader(infile)):
            if not i % 10:
                outcsv.writerow(row)