473,385 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 13001
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.