[Baypiggies] csv module question

Max Slimmer max at theslimmers.net
Wed Feb 23 02:20:03 CET 2011


>From the documentation:
DictWriter objects have the following public method:

DictWriter.writeheader()
Write a row with the field names (as specified in the constructor).


max


On Tue, Feb 22, 2011 at 4:26 PM, Jason Culverhouse
<jason at mischievous.org> wrote:
>
> 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
>
> _______________________________________________
> Baypiggies mailing list
> Baypiggies at python.org
> To change your subscription options or unsubscribe:
> http://mail.python.org/mailman/listinfo/baypiggies
>


More information about the Baypiggies mailing list