[Numpy-discussion] Problem with Numpy and array

Miguel de Val-Borro miguel.deval at gmail.com
Thu May 12 06:30:03 EDT 2011


On Thu, May 12, 2011 at 12:21:46PM +0200, Claudia Chan Yone wrote:
> Hi,
> 
> I have a problem with the Numpy module, but I think it is a very basic issue
> for many of you...
> I have a file with numerical data (2 columns, and 5 lignes) as :
> 1 2
> 3 4
> ... ...
> 
> And I woulid like to convert it in a matrix as :
> [[1,2]
> [3,4]
> ...]
> 
> My python script is :
> 
> fic=open('file.txt','r')
> ligne=fic.readlines()
> fic.close()

Try using the loadtxt function in numpy

>>> m = numpy.loadtxt('file.txt')
array([[ 1.,  2.],
        [ 3.,  4.]])

Miguel



More information about the NumPy-Discussion mailing list