[Baypiggies] csv module question

Glen Jarvis glen at glenjarvis.com
Tue Feb 22 23:16:19 CET 2011


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())

writer.writerow(headers)
for protein in data:
    row = [protein[h] for h in headers]
    writer.writerow(row)





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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/baypiggies/attachments/20110222/fd91bade/attachment.html>


More information about the Baypiggies mailing list