On Thu, Mar 17, 2011 at 8:53 AM, Dipo Elegbede <delegb...@dudupay.com>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: > > PARTY A A 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 <m...@timgolden.me.uk> 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