I have a large csv with the following header columns id
, type
,state
,location
and the following values:
124, preschool, Pennsylvania, Pittsburgh
421, secondary school, Ohio, Cleveland
213, primary school, California, Los Angeles
155, secondary school, Pennsylvania, Pittsburgh
etc...
The file is not ordered and I want a csv file for each type of school.
The answers that I found were regarding to ordered csv files, or splitting them after a specific number of rows.
EDIT: I discovered what I wanted:
import csv
csv_file = 'school.csv'
value = 'preschool'
with open(csv_file, 'rb') as csvfile:
spamreader = csv.reader(csvfile, delimiter=',', quotechar='|')
for row in spamreader:
if value in row:
with open(value + '.csv', 'ab') as myfile:
spamwriter = csv.writer(myfile)
spamwriter.writerow(row)
myfile.close()
and to keep the header columns I just copy paste them from the original one