473,385 Members | 1,890 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.

how to read in an array in text file?

Hi, what's the easy way of reading in a two-dimentional array from a text
file?
The text file looks like this:
0.1 1.1 2.3 12.1
0.3 33.0 4.1 1.1

I'm using Numeric Python now. Other solutions are also welcome. Thanks a
lot!
Yun
Jul 18 '05 #1
2 18408
"Yun Mao" <ma**@cis.upenn.edu> schrieb im Newsbeitrag
news:bo***********@netnews.upenn.edu...
Hi, what's the easy way of reading in a two-dimentional array from a text
file?
The text file looks like this:
0.1 1.1 2.3 12.1
0.3 33.0 4.1 1.1

I'm using Numeric Python now. Other solutions are also welcome. Thanks a
lot!
Yun


f = file("myfile", "r")
data = [line.split() for line in f]
f.close()

HTH

Ciao Ulrich
Jul 18 '05 #2
On Sun, 9 Nov 2003 12:17:39 +0100, "Ulrich Petri" <ul***@gmx.de> wrote:
"Yun Mao" <ma**@cis.upenn.edu> schrieb im Newsbeitrag
news:bo***********@netnews.upenn.edu...
Hi, what's the easy way of reading in a two-dimentional array from a text
file?
The text file looks like this:
0.1 1.1 2.3 12.1
0.3 33.0 4.1 1.1

I'm using Numeric Python now. Other solutions are also welcome. Thanks a
lot!
Yun
f = file("myfile", "r")
data = [line.split() for line in f]

# or (untested), to get floats when you access data[row][col]
data = [map(float,line.split()) for line in f]
f.close()


Hate to leave that "(untested)" ... so, simulating the file with StringIO...
from StringIO import StringIO
f = StringIO("""\ ... 0.1 1.1 2.3 12.1
... 0.3 33.0 4.1 1.1
... """) data = [map(float,line.split()) for line in f]
data [[0.10000000000000001, 1.1000000000000001, 2.2999999999999998, 12.1], [0.29999999999999999, 33.0
, 4.0999999999999996, 1.1000000000000001]]
for row in data:

... for colitem in row: print '%10.3f' %colitem,
... print
...
0.100 1.100 2.300 12.100
0.300 33.000 4.100 1.100

Regards,
Bengt Richter
Jul 18 '05 #3

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

Similar topics

19
by: ranjeet | last post by:
Hay Guys can you all suggest me the points on the below issue Problem : The thing is that I have the data some thing like this. 1486, 2168, 3751, 9074, 12134, 13944, 17983, 19173, 21190,...
7
by: Naren | last post by:
Hello All, Can any one help me in this file read problem. #include <stdio.h> int main() {
35
by: RyanS09 | last post by:
Hello- I am trying to write a snippet which will open a text file with an integer on each line. I would like to read the last integer in the file. I am currently using: file = fopen("f.txt",...
9
by: Adi | last post by:
Hello eveyone, I wanna ask a very simple question here (as it was quite disturbing me for a long time.) My problem is to read a file line by line. I've tried following implementations but still...
9
by: srikanth | last post by:
i have a text file like below, test.txt file (actually my test file file is with 10000 lines but here i tested with 3 lines) 3 06.09.2006 16:37:25 3 06.09.2006 16:40:02 3 06.09.2006 16:42:31...
6
by: xdeath | last post by:
Hi guys, i've currently got an assignment, whereby, im supposed to create 2 classes, a Vehicle superclass, and a Taxi subclass. Vehicle class needs to have Reg Number, model, price, and Taxi is...
7
by: bowlderster | last post by:
Hello, all. This is the text file named test.txt. 1041 1467 7334 9500 2169 7724 3478 3358 9962 7464 6705 2145 6281 8827 1961 1491 3995 3942 5827 6436 6391 6604 4902 1153 1292 4382 9421 1716...
2
by: RyanS09 | last post by:
Hi- I have read many posts with specific applications of reading in text files into arrays, however I have not been able to successfully modify any for my application. I want to take a text file...
13
by: rohit | last post by:
Hi All, I am new to C language.I want to read integers from a text file and want to do some operation in the main program.To be more specific I need to multiply each of these integers with another...
5
by: dm3281 | last post by:
Hello, I have a text report from a mainframe that I need to parse. The report has about a 2580 byte header that contains binary information (garbage for the most part); although there are a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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.