You can use csv.writer() to write rows of data to another CSV file, or use csv.DictWriter() to write dictionaries with row data. The latter does require that you specify fieldnames up front.
Copying across CSV rows could best be done straight when reading:
with open(inputfilename, 'rb') as ifh, open(outputfilename, 'wb') as ofh:
reader = csv.reader(ifh)
writer = csv.reader(ifh)
writer.writerows(reader)
Here writer.writerows() takes an iterable of rows; a reader object happens to be an iterable, so this copies data straight across. You can add additional dialect parameters t othe csv.reader() and csv.writer() constructors to change the format; say you read with delimiter='\t' (tab separated) and write out the default comma-separated format.
If you want to process each row in between, use a loop:
for row in reader:
# do something with row
writer.writerow(row)
The writer.writerow() (singular) method takes one row at a time to write out.
To copy across dictionary rows, you need to specify the fields. Use a csv.DictReader() object to produce the dictionaries, it'll handle fieldnames from the first CSV row for you:
with open(inputfilename, 'rb') as ifh, open(outputfilename, 'wb') as ofh:
reader = csv.DictReader(ifh)
writer = csv.DictWriter(ifh, fieldnames=reader.fieldnames)
writer.writerows(reader)
Here, the fieldnames parameter is taken straight from the csv.DictReader().fieldnames attribute. Again, process rows in a loop as required, same as with a regular csv.reader() / csv.writer() pair.
csv.DictReader()instead? It'd have handled the fields and dictionary creation automatically for you.dict(zip(fields, row))and go straight for the list rows.