472,364 Members | 1,564 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,364 software developers and data experts.

using split function

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

Nov 7 '06 #1
3 12845
ina

am***********@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

Nov 7 '06 #2
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
Nov 7 '06 #3
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
Nov 7 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Alexis | last post by:
Hi, I need to transform one xml document into a second xml document. They both have many nodes so xslt works fine, but there is one node I have not figure out how to transform. Here it is:...
11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
5
by: Vamsi | last post by:
Hi, I am trying a basic opearation of splitting a multiline value to an array of single lines(Actually making Address into AddressLine1, AddressLine2). I used Environment.NewLine in split, I...
1
by: ratnakarp | last post by:
Hi, I have a search text box. The user enters the value in the text box and click on enter button. In code behind on button click i'm writing the code to get the values from the database and...
3
by: Ben | last post by:
Hi I am creating a dynamic function to return a two dimensional array from a delimeted string. The delimited string is like: field1...field2...field3... field1...field2...field3......
6
by: ransoma22 | last post by:
I developing an application that receive SMS from a connected GSM handphone, e.g Siemens M55, Nokia 6230,etc through the data cable. The application(VB.NET) will receive the SMS automatically,...
11
by: pmarisole | last post by:
I am trying to use the vbscript "split" function on a multi-select field. I am trying to do a mass update of several records at a time. I am getting an error and I'm not sure what to do. Here is...
2
by: David | last post by:
Hi, Could PHP be used to take a txt file (or set of txt files) and add a string of characters every X number of words or characters? Say a txt file with 50,000 characters/5,000 words how would...
1
by: Richard Harter | last post by:
On Fri, 27 Jun 2008 09:28:56 -0700 (PDT), pereges <Broli00@gmail.comwrote: There are some obvious questions that should be asked, e.g., is the contents of your array already sorted as your...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.