[Numpy-discussion] Read array from file

lorenzo bolla lbolla at gmail.com
Fri Mar 14 06:21:32 EDT 2008


what about numpy.loadtxt?

In [9]: numpy.loadtxt('test.dat', skiprows=5)
Out[9]:
array([[ 15.44261,  12.05814,  54.43124],
       [ 15.54899,  12.00075,  53.85503],
       [ 15.95802,  11.92959,  53.88939],
       [ 15.84085,  12.00235,  54.43274],
       [ 15.53889,  11.16645,  54.51649],
       [ 15.57673,  11.10806,  53.96009],
       [ 16.10059,  11.06809,  53.87672],
       [ 16.04238,  11.11615,  54.47454],
       [ 15.78142,  11.82206,  53.33932],
       [ 16.13055,  11.75515,  53.3731 ]])

hth,
L.

On Fri, Mar 14, 2008 at 11:12 AM, Dimitrios Kiousis <dkiousis at gmail.com>
wrote:

> Hello python users,
>
> I have an input file consisting of string-lines and float-lines. This is
> how it looks:
>
> # vtk DataFile Version 3.0
> VTK file exported from FEAP
> ASCII
> DATASET UNSTRUCTURED_GRID
> POINTS    6935  FLOAT
>   15.44261   12.05814   54.43124
>   15.54899   12.00075   53.85503
>   15.95802   11.92959   53.88939
>   15.84085   12.00235   54.43274
>   15.53889   11.16645   54.51649
>   15.57673   11.10806   53.96009
>   16.10059   11.06809   53.87672
>   16.04238   11.11615   54.47454
>   15.78142   11.82206   53.33932
>   16.13055   11.75515   53.37313
>   .................
>
> I want to read the first 5 string lines, and then store the float data
> (coordinates) into an array.
> It took me some time to figure this out but this is the script with which
> I came out:
>
>  # Read and write the first information lines
> for i in range(0,5):
>   Fdif.write( Fpst.readline() )
>
> # Read and write coordinates
> # --------------------------
>
> # Initialization
> coords = zeros( (nnod,3), float )
>
> for i in range(0,nnod):
>   # Read line
>   x = Fref.readline() # Read lines
>   x = x.split()       # Split line to strings
>   x = map ( float,x ) # Convert string elements to floats
>   x = array ( x )     # Make an array
>   for j in range (0,3):
>     coords[i,j] = x[j]
>
> It seems quite complicated to me, but I haven't figure any nicer way.
> Could you tell me if what I am doing looks reasonanble or if there are any
> other solutions?
> Do I really need to initiallize coords?
>
> Thanks in advance,
> Dimitrios
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>
>


-- 
Lorenzo Bolla
lbolla at gmail.com
http://lorenzobolla.emurse.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20080314/2e4d86c9/attachment.html>


More information about the NumPy-Discussion mailing list