table from csv file

J dreadpiratejeff at gmail.com
Sat Jan 9 17:34:29 CET 2010


On Sat, Jan 9, 2010 at 05:26, Jon Clements <joncle at googlemail.com> wrote:

>> reader = open('C:/test.txt','rb')
>> data = csv.DictReader(reader,restval='000',restkey='Misc')
>
> [snip]
>
> DictReader works, but what use to bug me was the fact you couldn't
> then output the cols in the 'correct' order afterwards, so you had
> to store the header row anyway to re-order the rows...
> (although admittedly this doesn't affect the OP's question).
>
> However, I see that 2.6+ offers .fieldnames on DictReader objects.

At a guess, and I stipulate that because I am just learning about all
this, I'd say that's because DictReader returns a dictionary for each
row retrieved...  It DOES take the first row and make those the dict
keys, but after that, it just returns a Dict for each rows and
dictionaries seem to be randomly ordered.

It may not be that great if you're doing  something that needs order,
but all he's doing is pulling arbitrary columns from a file and
printing as a table, so that works great in this case...

It was quite interesting messing around with that yesterday.  I saw
the question, and it looked interesting, so I went and learned and
applied ;-)  I'm rather smugly satisfied with myself ...

Cheers,

Jeff

-- 

Samuel Goldwyn  - "I'm willing to admit that I may not always be
right, but I am never wrong." -
http://www.brainyquote.com/quotes/authors/s/samuel_goldwyn.html



More information about the Python-list mailing list