[SciPy-User] Loadtxt vs genfromtxt

Warren Weckesser warren.weckesser@enthought....
Thu Mar 1 07:12:11 CST 2012


On Thu, Mar 1, 2012 at 4:14 AM, Bjorn Burr Nyberg <
bjorn.burr.nyberg@gmail.com> wrote:

>
> Hi,
> I have a general question about loading data into numpy as I want to
> compare numpy and r by loading the juraset.dat ASCII file from the gstat
> package. Reading the support documents I have decided that it is better to
> use the loadtxt function as I do not have any missing data as useful by the
> genfromtxt function. However I receive this error when running loadtxt:
>
> File ..... Numpy\lib\npyio.py, line 796, in loadtxt
> Items = [conv(Val) for (conv,val) in zip(converts,Vals)]
> ValueError: invalid literal for float()
>
> Using the same parameters but with genfromtxt works, although the first
> entry of the array is Nan(not a numeric - expected a header like in a data
> frame of r). I suppose I was wondering if there was any way to save header
> data of an array whereby one could simply call that header for the data?Do
> I just have to remember the data associated with each column and call using
> data[]? Even loading the data as x,y,z = loadtxt is problematic when there
> are several columns associated with the data that I do not necessarily
> remember offhand.
>
> Thanks for any advice and with your patience as I'm rather new to Numpy.
> Nyberg
>


Bjorn,

I don't see the text file 'juraset.dat' in the gstat package
(gstat_1.0-10), but google finds this:
 http://www.ualberta.ca/~jbb/files/juraset.dat

If that is your file, you can read it with genfromtxt like this:


In [1]: data = genfromtxt('juraset.dat', skiprows=26, names=True)

In [2]: data[0]
Out[2]: (2.386, 3.077, 3.0, 3.0, 1.74, 25.72, 77.36, 9.32, 38.32, 21.32,
92.56)

In [3]: data['Zn'][:3]
Out[3]: array([ 92.56,  73.56,  64.8 ])

In [4]: data.dtype.names
Out[4]: ('X', 'Y', 'Rock', 'Land', 'Cd', 'Cu', 'Pb', 'Co', 'Cr', 'Ni', 'Zn')


The option 'names=True' tells genfromtxt to create a structured array,
using the fields in first line (after skiprows) as the field names for the
array.

Warren
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20120301/8963b266/attachment.html 


More information about the SciPy-User mailing list