using split function

amitsoni.1984 at gmail.com amitsoni.1984 at gmail.com
Tue Nov 7 07:09:02 EST 2006


Thanks a lot, I am done with that part. But now I am facing another
problem. I am using the code given below where A is a matrix and row is
a sequence. But it gives following error:

-------- error------
    A[a,:]=row
ValueError: setting an array element with a sequence.

--------------code----------------
#!/usr/bin/python
import numpy
file1 = open('matrix.txt', 'r')

count =  0
a=0
b=0
c=0
d=0
e=0
A = numpy.zeros([4,4])
while 1:
    lineStr = file1.readline()
    if not(lineStr):
        break

    count = count + 1
    row=lineStr.split()
    if count<=4:
        A[a,:]=row
        a=a+1
    elif count<=8:
        B[b,:]=row
        b=b+1
    elif count<=12:
        C[c,:]=row
        c=c+1
    elif count<=16:
        D[d,:]=row
        d=d+1
    elif count<=20:
        E[e,:]=row
        e=e+1

file1.close()
---------end of code-------------

is there any way to change a sequence to array?
thank you
Amit

Gabriel Genellina wrote:
> > > 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.
>
> A few hints:
> - don't use "file" as a name - it shadows the builtin "file" type
> - matrix.close() won't work, perhaps you meant file.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".
>
> Oh, so *that's* why you build it using standard_normal and then
> overwrite the contents!
>
>
> --
> Gabriel Genellina
> Softlab SRL
>
> __________________________________________________
> Correo Yahoo!
> Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
> ¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar




More information about the Python-list mailing list