Re: [Tutor] CSV Ouptut concern...
The counter is there so that it limits the iteration to only the first rows, i actually left it there because i used it earlier to print out specific rows. On Thu, Mar 17, 2011 at 2:04 PM, Joel Goldstick wrote: > > > On Thu, Mar 17, 2011 at 8:53 AM, Dipo Elegbede wrote: > >> >> Thanks Tim, that worked like magic. >> >> I now have another challenge on the same file, in this case, i am trying >> to extract just a column as PARTY, I successfully wrote the header but >> instead of having each element in a single excel block like this: >> >> A >> ACD >> ACN >> ACPN >> AD >> ADC >> ALP >> ANPP >> APGA >> APS >> ARP >> BNPP >> CAP >> CDC >> CPC >> CPN >> CPP >> DFPF >> DPP >> >> It is spelt out on different block like this: >> >> PARTYAA C D A C N A C P N A D A D C A L P A N P P A >> P G A A P S A R P B N P P C A P C D C C P C C P N C P P D >> F P F D P P >> >> >> >> >> what could be wrong with this code: >> >> import csv >> >> reader = csv.reader(open('stateparty.csv')) >> counter = 1 >> header = ["PARTY"] >> fh = open('stateparty3.csv','wb') >> writer = csv.writer(fh) >> writer.writerow(header) >> >> for row in reader: >> if counter == 1: >> parties = row[1:-1] >> for party in parties: >> writer.writerow([party]) >> counter += 1 >> fh.close() >> >> Thanks for your responses as anticipated. >> >> >> >> >> >> >> >> >> >> On Thu, Mar 17, 2011 at 1:00 PM, Tim Golden wrote: >> >>> On 17/03/2011 11:56, Dipo Elegbede wrote: >>> 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. >>> >>> Open the file in binary mode: >>> >>> fh = open('stateparty2.csv','wb') >>> >>> TJG >>> ___ >>> Tutor maillist - Tutor@python.org >>> To unsubscribe or change subscription options: >>> http://mail.python.org/mailman/listinfo/tutor >>> >> >> >> >> -- >> Elegbede Muhammed Oladipupo >> OCA >> +2348077682428 >> +2347042171716 >> www.dudupay.com >> Mobile Banking Solutions | Transaction Processing | Enterprise Application >> Development >> >> ___ >> Tutor maillist - Tutor@python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> >> > You want row[1] from each iteration of row. So instead of this: > > > for row in reader: > if counter == 1: > parties = row[1:-1] > for party in parties: > writer.writerow([party]) > counter += 1 > fh.close() > > > Do this: > > > for row in reader: > if counter == 1: > party = row[1:2] > write.writerow(party) >counter += 1 > > > What is the counter for? > > > > -- > Joel Goldstick > > -- Elegbede Muhammed Oladipupo OCA +2348077682428 +2347042171716 www.dudupay.com Mobile Banking Solutions | Transaction Processing | Enterprise Application Development ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] CSV Ouptut concern...
On Thu, Mar 17, 2011 at 8:53 AM, Dipo Elegbede wrote: > > Thanks Tim, that worked like magic. > > I now have another challenge on the same file, in this case, i am trying to > extract just a column as PARTY, I successfully wrote the header but instead > of having each element in a single excel block like this: > > A > ACD > ACN > ACPN > AD > ADC > ALP > ANPP > APGA > APS > ARP > BNPP > CAP > CDC > CPC > CPN > CPP > DFPF > DPP > > It is spelt out on different block like this: > > PARTYAA C D A C N A C P N A D A D C A L P A N P P A P > G A A P S A R P B N P P C A P C D C C P C C P N C P P D F > P F D P P > > > > > what could be wrong with this code: > > import csv > > reader = csv.reader(open('stateparty.csv')) > counter = 1 > header = ["PARTY"] > fh = open('stateparty3.csv','wb') > writer = csv.writer(fh) > writer.writerow(header) > > for row in reader: > if counter == 1: > parties = row[1:-1] > for party in parties: > writer.writerow([party]) > counter += 1 > fh.close() > > Thanks for your responses as anticipated. > > > > > > > > > > > On Thu, Mar 17, 2011 at 1:00 PM, Tim Golden wrote: > >> On 17/03/2011 11:56, Dipo Elegbede wrote: >> >>> 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. >>> >> >> Open the file in binary mode: >> >> fh = open('stateparty2.csv','wb') >> >> TJG >> ___ >> Tutor maillist - Tutor@python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> > > > > -- > Elegbede Muhammed Oladipupo > OCA > +2348077682428 > +2347042171716 > www.dudupay.com > Mobile Banking Solutions | Transaction Processing | Enterprise Application > Development > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > You want row[1] from each iteration of row. So instead of this: for row in reader: if counter == 1: parties = row[1:-1] for party in parties: writer.writerow([party]) counter += 1 fh.close() Do this: for row in reader: if counter == 1: party = row[1:2] write.writerow(party) counter += 1 What is the counter for? -- Joel Goldstick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] CSV Ouptut concern...
Thanks Tim, that worked like magic. I now have another challenge on the same file, in this case, i am trying to extract just a column as PARTY, I successfully wrote the header but instead of having each element in a single excel block like this: A ACD ACN ACPN AD ADC ALP ANPP APGA APS ARP BNPP CAP CDC CPC CPN CPP DFPF DPP It is spelt out on different block like this: PARTYAA C D A C N A C P N A D A D C A L P A N P P A P G A A P S A R P B N P P C A P C D C C P C C P N C P P D F P F D P P what could be wrong with this code: import csv reader = csv.reader(open('stateparty.csv')) counter = 1 header = ["PARTY"] fh = open('stateparty3.csv','wb') writer = csv.writer(fh) writer.writerow(header) for row in reader: if counter == 1: parties = row[1:-1] for party in parties: writer.writerow([party]) counter += 1 fh.close() Thanks for your responses as anticipated. On Thu, Mar 17, 2011 at 1:00 PM, Tim Golden wrote: > On 17/03/2011 11:56, Dipo Elegbede wrote: > >> 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. >> > > Open the file in binary mode: > > fh = open('stateparty2.csv','wb') > > TJG > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Elegbede Muhammed Oladipupo OCA +2348077682428 +2347042171716 www.dudupay.com Mobile Banking Solutions | Transaction Processing | Enterprise Application Development ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] CSV Ouptut concern...
On 17/03/2011 11:56, Dipo Elegbede wrote: 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. Open the file in binary mode: fh = open('stateparty2.csv','wb') TJG ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] CSV Ouptut concern...
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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor