using split function

ina erinhouston at gmail.com
Mon Nov 6 23:38:15 EST 2006


amitsoni.1984 at gmail.com wrote:
> Hi,
>
> I have to write a code in python to read a matrix from a text file and
> for that i am using following code. But it gives an error saying
> "NameError: name 'split' is not defined". Can anyone help me with this.
> -------------------------------------------------
> #!/usr/bin/python
> import numpy
> file = open('matrix.txt', 'r')
>
> count =  0
> ra = numpy.random
> A = ra.standard_normal((4,4))
> while 1:
>     lineStr = file.readline()
>     if not(lineStr):
>         break
>
>     count = count + 1
>     row=split(lineStr)
>     A[count,:]=row
>
> matrix.close()
> -----------------------------------------------------
> Also, i want to initialize the matrix A by zeros, but using A=zeros([4,
> 4]) was giving a similar error "NameError: name 'zeros' is not
> defined".
>
> Thank you
> Amit
This is how I would do it.
for lineStr in file:
...row = lineStr.split()

you could also use str.split(lineStr) but the other way is cleaner




More information about the Python-list mailing list