I have a csv file with, say, 50 rows of data, and I would like to split it into separate csv files for each row, which includes first row (header) and the the relevant row.
E.g. file 1 contains: row1, row2, file 2 contains: row1, row3, file 3 contains: row1, row4
And so on.
Currently working with:
import csv
counter = 1
with open('mock_data.csv', 'r', newline='') as csvfile:
reader = csv.reader(csvfile)
for row in reader:
filename = "file_%s" % str(counter)
with open(filename, 'w') as csvfile_out:
writer = csv.writer(csvfile_out)
writer.writerow(row)
counter = counter + 1
I'm currently getting 'csvfile_out' not defined.
a) Am I even approaching this correctly b) Any ideas why csvfile_out isn't being defined?