On Fri, Jan 8, 2010 at 13:55, Jon Clements <jon...@googlemail.com> wrote: > On Jan 8, 5:59 pm, marlowe <marlowequ...@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') print "Options are: Date, Open, Close, High, Low, Volume, Adj Close" column = raw_input('What do you want? (comma delimited)?') choice = column.split(',') f = 12 print ''.join([s.center(f) for s in choice]) for row in data: print ''.join([(row.get(s)).center(f) for s in choice]) -- Mike Ditka - "If God had wanted man to play soccer, he wouldn't have given us arms." - http://www.brainyquote.com/quotes/authors/m/mike_ditka.html -- http://mail.python.org/mailman/listinfo/python-list