table from csv file
joncle at googlemail.com
Sat Jan 9 11:26:48 CET 2010
On Jan 8, 8:31 pm, J <dreadpiratej... at gmail.com> wrote:
> On Fri, Jan 8, 2010 at 13:55, Jon Clements <jon... at googlemail.com> wrote:
> > On Jan 8, 5:59 pm, marlowe <marlowequ... at hotmail.com> wrote:
> >> I am trying to create a table in python from a csv file where I input
> >> which columns I would like to see, and the table only shows those
> >> columns. I have attached an example of the csv file i am using, and
> >> some of the code I have written. I am having trouble converting
> >> variables between lists, dictionaries and tuples. Is there a name for
> >> what I am attempting to do? any help to get me on the right track with
> >> this is appreciated.
> >> test.csv
> I had to edit that and comma delimit it, because cut and paste gave me
> random numbers/types of whitespace...
> [code snipped]
> > This might be a useful starting point (I'm guessing this is what
> > you're after...)
> > Let's assume your 'CSV' file is tab separated as it's certainly not
> > comma separated :)
> > import csv
> > csvin = csv.reader(open('test.csv'), delimiter='\t')
> > header = dict( (val.strip(),idx) for idx, val in enumerate(next
> > (csvin)) )
> > We can use header as a column name->column index lookup eg header
> > ['Open'] == 1
> > from operator import itemgetter
> > wanted = ['Open', 'Close'] # Although you'll want to use raw_input and
> > split on ','
> > getcols = itemgetter(*[header[col] for col in wanted])
> > getcols is a helper function that'll return a tuple of the columns in
> > the requested order...
> > for row in csvin:
> > print getcols(row)
> > Loop over the rest of the file and output the required columns.
> As someone who knows just enough to be dangerous... what about this:
> import csv
> reader = open('C:/test.txt','rb')
> data = csv.DictReader(reader,restval='000',restkey='Misc')
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.
More information about the Python-list