using split function

A

amitsoni.1984

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
 
I

ina

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
 
D

Dennis Lee Bieber

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)

row = lineStr.split()
A[count,:]=row

matrix.close()

Correct... I think you'll find that numpy.zeros() IS defined
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
G

Gabriel Genellina

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()?

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
 
A

amitsoni.1984

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 said:
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()?

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,230
Latest member
LifeBoostCBD

Latest Threads

Top