[Tutor] csv DictReader/Writer question
Ara Kooser
ghashsnaga at gmail.com
Thu Oct 21 22:50:41 CEST 2010
Peter,
Thank you for your response. I did try reading the documentation but I
missed something (or several somethings in this case). So what I see in the
code you supplied is:
with open(source, "rb") as instream:
reader = csv.DictReader(instream, skipinitialspace=True)
destfieldnames = list(reader.fieldnames)
destfieldnames.remove("Species2")
destfieldnames.remove("Protein ID2")
So this reads the csv file in but I need to run it to see what
skipinitialspace does. Then it reads in the header line and removes the
Species2 and Protein ID2. Does this remove all the data associated with
those columns? For some reason I thought I had to bring these into a
dictionary to manipulate them.
with open(dest, "wb") as outstream:
writer = csv.DictWriter(outstream,
destfieldnames,extrasaction="ignore")
writer.writer.writerow(destfieldnames)
writer.writerows(reader)
I think the first line after the open writes the field names to the file and
the follow lines write the data is that correct? I am going to run the code
as soon as I get home.
ara
--
Quis hic locus, quae regio, quae mundi plaga. Ubi sum. Sub ortu solis an sub
cardine glacialis ursae.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20101021/8bb57514/attachment.html>
More information about the Tutor
mailing list