Woeitg Woeitg - 1 month ago 5
Python Question

Python doesn't go automatically to next line when writing to CSV file

I am trying to open a source CSV file (

source.csv
) and decompose it into several CSV files according to first column name. I show it with this example:

Content of
source.csv
:

2016-11,a
2016-11,b
2016-12,a
2016-12,b
2016-12,c


and I expect the program to create two files with
2016-11.csv
and
2016-12.csv
names:

expected content of
2016-11.csv
:

2016-11,a
2016-11,b


expected content of
2016-12.csv
:

2016-12,a
2016-12,b
2016-12,c





I developed this code:

import csv
path1='/home/sourcefilepath/'
path2='/home/targetpath/'
filename='source.csv'

with open(path1+filename) as f:
reader = csv.reader(f)
for row in reader:
date=row[0]
with open(path2+date+'.csv', 'w') as csvfile:
writer = csv.writer(csvfile, delimiter=',')
writer.writerow(row)


The problem is it just write the first line of each file and doesn't automatically goes to next line while writing. How can I fix this issue?

Answer

You're overwriting the contents of your file each time you open them with the w flag, try instead by grouping your rows with itertools.groupby:

import csv
import itertools

with open(path1 + filename) as f:
    reader = csv.reader(f)
    for date, rows in itertools.groupby(reader, lambda row: row[0]):
        with open(path2 + date + '.csv', 'w') as csvfile:
            writer = csv.writer(csvfile, delimiter=',')
            writer.writerows(rows)