[Matplotlib-users] parsing tab separated files into dictionaries - alternative to genfromtxt?

2009-11-11 Thread per freem
hi all,

i've been using genfromtxt to parse tab separated files for plotting
purposes in matplotlib. the problem is that genfromtxt seems to give
only two ways to access the contents of the file: one is by column,
where you can use:

d = genfromtxt(...)

and then do d['header_name1'] to access the column named by
'header_name1', d['header_name2'] to access the column named by
'header_name2', etc.  Or it will allow you to traverse the file line
by line, and then access each header by number, i.e.

for line in d:
  field1 = d[0]
  field2 = d[1]
  # etc.

the problem is that the second method relies on knowing the order of
the fields rather than just their name, and the first method does not
allow line by line iteration.
ideally what i would like is to be able to traverse each line of the
parsed file, and then refer to each of its fields by header name, so
that if the column order in the file changes my program will be
unaffected:

for line in d:
  field1 = ['header_name1']
  field2 = ['header_name2']

is there a way to do this using standard matplotlib/numpy/scipy
utilities? i could write my own code to do this but it seems like
something somebody probably already thought of a good representation
for and has implemented a more optimized version than i could write on
my own. does such a thing exist?

thanks very much

--
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
___
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users


Re: [Matplotlib-users] parsing tab separated files into dictionaries - alternative to genfromtxt?

2009-11-11 Thread Mike Anderson
 ideally what i would like is to be able to traverse each line of the
 parsed file, and then refer to each of its fields by header name, so
 that if the column order in the file changes my program will be
 unaffected:



What you want is a DictReader.

For a quick example of me using that,
--
aReader = csv.DictReader(open(inputFilename, 'r'), delimiter='\t')

yValues = {}  # Will be a dictionary of lists  (these are lists of y- 
values)
xValues = []  # List of x values (timestamps)

for row in aReader:
xValues.append(datetime.datetime(*(time.strptime(row 
['TimeStamp'],timeStampFormat)[0:6])))
for field in sorted(row,reverse=True): # Read from last  
to first in field list
if field=='TimeStamp': continue  # Column of x-data
if not field in yValues.keys():
yValues[field] = [nt(row[field])]   # Start list of 
the values  
in this column
else:
yValues[field].append(int(row[field])) # Add to list of 
values in  
this column
--


Mike

--
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
___
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users