473,799 Members | 2,903 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.tx t', 'r')

count = 0
ra = numpy.random
A = ra.standard_nor mal((4,4))
while 1:
lineStr = file.readline()
if not(lineStr):
break

count = count + 1
row=split(lineS tr)
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 13025
ina

am***********@g mail.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.tx t', 'r')

count = 0
ra = numpy.random
A = ra.standard_nor mal((4,4))
while 1:
lineStr = file.readline()
if not(lineStr):
break

count = count + 1
row=split(lineS tr)
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(lineS tr) 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.tx t', '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.spl it()
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
319
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: Node in Input File. <FullAddress>JOHN SMITH@19495 BISCAYNE BLVD@MIAMI@FL/33180@USA</FullAddress>
11
6604
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 where the job is running, the job runs sucessfully, PDF files got generated, everything is good. If I scheduled the job to run at the time that I am not logged into the server, Access is not able to print to the printer. The error is pretty...
5
57205
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 could get only 1st line, but it is not returning 2nd line. here's code: string address = null; string ssep = Environment.NewLine; char sep = ssep.ToCharArray();
1
6429
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 binding it to a repeater control. This repeater control has multiple text boxes and buttons. Can you please tell me how can i do paging in this case ? I'm posting my code below. The problem is that if i click on "AdjustThisAd" button, it opens...
3
9671
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... field1...field2...field3...
6
17207
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, process and output to the screen in my application when a message arrived. But the problem is how do I read the SMS message immediately when it arrived without my handphone BeEPINg for new message ? I read up the AT commands, but when getting down...
11
4442
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 the code if someone could help... strID = split(request.form("proj"), ", ") projstat = split(request.form("rojstat"),",") impr = split(request.form("impr"),",") idate =...
2
2191
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 you go about adding a string of characters every 5,000 characters or 500 words. To improve on this I'd want to if using characters as the guide to use
1
4934
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 example implies. If they are then all you need to do is find the element at index k = n/2 and then increase k until you find an element that differs from A. Then k is the number of left children and n-k is the number of right children. (Fencepost...
0
9686
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10250
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10222
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10026
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9068
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4139
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.