Blank rows resulting from simple csv script

D

Drew

Hi all -

I've written a simple script to read a .csv file and then write out
rows to a new file only if the value in the 4th column is a 0. Here's
the code:

import csv

reader = csv.reader(open('table_export.csv','rb'))

writer = csv.writer(open('new_jazz.csv','w'))

for row in reader:
if row[3] == '0':
writer.writerow(row)

This is writing out the correct rows, however it is writing a blank
row between each of the rows written out. Any ideas?

Thanks,
Drew
 
D

Diez B. Roggisch

Drew said:
Hi all -

I've written a simple script to read a .csv file and then write out
rows to a new file only if the value in the 4th column is a 0. Here's
the code:

import csv

reader = csv.reader(open('table_export.csv','rb'))

writer = csv.writer(open('new_jazz.csv','w'))

for row in reader:
if row[3] == '0':
writer.writerow(row)

This is writing out the correct rows, however it is writing a blank
row between each of the rows written out. Any ideas?

The modes aren't compatible - either use rb, wb or r,w

Diez
 
J

John Machin

Drew said:
Hi all -

I've written a simple script to read a .csv file and then write out
rows to a new file only if the value in the 4th column is a 0. Here's
the code:

import csv

reader = csv.reader(open('table_export.csv','rb'))

writer = csv.writer(open('new_jazz.csv','w'))

for row in reader:
if row[3] == '0':
writer.writerow(row)

This is writing out the correct rows, however it is writing a blank
row between each of the rows written out. Any ideas?

The modes aren't compatible - either use rb, wb or r,w

Bzzzzt! *ALWAYS* use binary mode for both reading and writing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top