I am trying to copy rows from one CSV to another, and also to remove rows after being copied from the first file.
Please see the code, it is working for only copying specific rows from one CSV file to another, but it is not removing the rows from file 1.
import csv
import os
File1 = 'in.csv'
File2 = 'out.csv'
with open(File1, "r") as r, open(File2, "a") as w:
reader = csv.reader(r, lineterminator = "\n")
writer = csv.writer(w, lineterminator = "\n")
for counter,row in enumerate(reader):
if counter<0: continue
if counter>50:break
writer.writerow(row)
with open(File1, "w") as r:
reader = csv.writer(r, lineterminator = "\n")
for counter,row in enumerate(reader):
if counter<0: continue
if counter>50:break
reader.writerow(row)
Please let me know how this would work after coping the rows to the File 2 and then remove those rows from File 1.
Here is the error:
for counter,row in enumerate(reader):
TypeError: '_csv.writer' object is not iterable