[Baypiggies] csv module question
Jason Culverhouse
jason at mischievous.org
Wed Feb 23 01:26:30 CET 2011
On Feb 22, 2011, at 2:16 PM, Glen Jarvis wrote:
> Vikram,
> I took another stab at this, sorting the headers and the data by the keys. This makes the columns in the order of the columns:
>
> data = (
> {'Protein Name': 'PE2R2_HUMAN', 'gi Number': '38258920', 'etc': 'blah'},
> {'Protein Name': 'APAF_HUMAN', 'gi Number': '20141188', 'etc': 'blah2'}
> )
>
> headers = sorted(data[0].keys())
>
# ^ reuse....
# use http://docs.python.org/library/csv.html#csv.DictWriter
writer = csv.DictWriter(out, keys)
for protein in data:
writer.writerow(protein)
Jason
>
>
>
>
>
> On Tue, Feb 22, 2011 at 1:25 PM, Vikram K <kpguy1975 at gmail.com> wrote:
> Consider the following python code which works perfectly:
>
> fout = open ('1A_out.csv','w')
>
> writer = csv.writer(fout)
>
> writer.writerow(('Protein Name','gi Number', 'Peptide', 'Residue',
> 'Position in Protein', 'length', 'Window''))
>
> I am writing the values in the csv file using a loop. Everything works fine.
>
> Now in each row of the csv file i generated i need to append 36 values in 36 separate fields which i have captured in a dictionary data type. Will i have to write out all the extra 36 fields names in the following line or is there some better way?:
>
> writer.writerow(('Protein Name','gi Number', 'Peptide', 'Residue',
> 'Position in Protein', 'length', 'Window''))
>
>
>
>
> _______________________________________________
> Baypiggies mailing list
> Baypiggies at python.org
> To change your subscription options or unsubscribe:
> http://mail.python.org/mailman/listinfo/baypiggies
>
>
>
> --
> Things which matter most must never be at the mercy of things which matter least.
>
> -- Goethe
> _______________________________________________
> Baypiggies mailing list
> Baypiggies at python.org
> To change your subscription options or unsubscribe:
> http://mail.python.org/mailman/listinfo/baypiggies
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/baypiggies/attachments/20110222/4740ac11/attachment-0001.html>
More information about the Baypiggies
mailing list