[Tutor] CSV Ouptut concern...
Dipo Elegbede
delegbede at dudupay.com
Thu Mar 17 12:56:43 CET 2011
i wrote a code for extracting information from a csv file into another csv
file.
it worked well but i have an immediate challenge i can't seem to fix.
the new file that is created has an row and then an empty row and then a row
all through the file. how can i make the empty rows not be part of the file.
here is the code:
import csv
reader = csv.reader(open('stateparty.csv'))
counter = 1
fh = open('stateparty2.csv','w')
writer = csv.writer(fh)
for row in reader:
if counter == 1:
parties_row = row
elif counter > 2:
for index, column in enumerate(row[1:-1]):
if column == "1":
writer.writerow([row[0],parties_row[index+1]])
counter += 1
fh.close()
and the output has something like this:
Benue,ACN
Benue,ANPP
Benue,APGA
Benue,CPC
Benue,PDP
Kogi,ACN
Kogi,ADC
Kogi,ANPP
Kogi,APGA
Kogi,CDC
Kogi,CPC
Kogi,DPP
i am not expecting the spaces in between each line, kindly help with the
challenge...
thanks.
--
Elegbede Muhammed Oladipupo
OCA
+2348077682428
+2347042171716
www.dudupay.com
Mobile Banking Solutions | Transaction Processing | Enterprise Application
Development
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110317/24d067c0/attachment.html>
More information about the Tutor
mailing list