On Wed, Aug 12, 2009 at 10:47 AM, per freem <perfr...@gmail.com> wrote:

> hi all,
>
> thanks for these comments. i tried loadtxt and genfromtxt and am
> having similar problems. my data looks like this:
>
> my;header1      myheader-2_a    myheader-2_b
> a:5-X:b 3;0;5;0;0;0     3.015
> c:6-Y:d 0;0;0;0;0;0     2.5
>
> i simply want to read these in, and have all numbers be read in as
> floats and all things that don't look like numbers (in this case the
> first and second columns) to be parsed in as strings.
>
> i tried:
>
> data = genfromtxt(myfile, delimiter='\t', dtype=None)
>
> (if i don't specify dtype=None, it reads everything as NaN)
>
> the first problem is that with dtype=None all the entries are parsed
> as strings. i'd like to be able to read in the unambiguously numeric
> values as numbers (column 3 in this case.)


That's because you aren't skipping the header row, so it's trying to infer
the proper dtype from the header names -- hence, string.  If you didn't want
to use the header names, you could use skiprows=1.


> the second problem is that if i try to use headers as column names using:
>
> data = genfromtxt(myfile, delimiter='\t', dtype=None, names=True)
>
> then it converts my headers into different strings:
>
> >> data
> array([('a:5-X:b', '3;0;5;0;0;0', 3.0150000000000001),
>       ('c:6-Y:d', '0;0;0;0;0;0', 2.5)],
>      dtype=[('myheader1', '|S7'), ('myheader2_a', '|S11'),
> ('myheader2_b', '<f8')])
>
> i would only like to refer to my headers using this notation:
>
> data['my;header1']
>

try adding: deletechars=''

If you really just want the values from specific columns, you can also pass
in a list of column numbers (or names) to keep:

arr = np.genfromtxt('test.txt', delimiter='\t', dtype=None, names=True,
deletechars='', usecols=[2])

Ryan

-- 
Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma
------------------------------------------------------------------------------
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

Reply via email to