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.
You can use DictReader too...
import csv
counter = 1
with open('mock_data.csv', 'r') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
filename = "file_%s" % str(counter)
with open(filename, 'w') as csvfile_out:
writer = csv.DictWriter(csvfile_out, fieldnames=reader.fieldnames)
headers = dict((n, n) for n in reader.fieldnames)
writer.writerow(headers)
writer.writerow(row)
counter = counter + 1