[Tutor] Importing file data into Python arrays
Alistair McGowan
bigal@geosci.uchicago.edu
Mon, 27 May 2002 16:02:53 -0500
--============_-1189589119==_============
Content-Type: multipart/alternative; boundary="============_-1189589119==_ma============"
--============_-1189589119==_ma============
Content-Type: text/plain; charset="us-ascii" ; format="flowed"
Dear Python Wranglers,
Being of a scientific bent, I need to handle
data in arrays to do things like resampling and general calculations.
I am used to BASIC arrays which I can read data into from other files
with no problem. However, I am being defeated by the Python
readline(s) for file import. I can do things within Python OK, such
as the example below lifted from a help page and modified.
import array
arr=[]
for i in range(5):
arr.append([])
for j in range(10):
arr[i].append(i*j*2)
print arr[:]
s = arr [4]
a = s[1]
b = s[2]
print a-b
And I can splice away and reassign values fine.
However, when I try to read in data from a file as shown below, I end
up with the lines, which I can't manipulate.
import array
arr=[]
inp = open ("dat.txt","r")
#read line into array
for line in inp.readlines():
arr.append(line)
print arr [0:3]
which gives
['1 2 3\n', '4 5 6\n', '7 8 9']
as output
I have tried using the suggested
num = int(string), but I the "/n" can't be dealt with from the space
delimited simple text file I am using to test this, which is appended
to this mail.
I have consulted my Python Essential Reference, but I am failing to
see the way to deal with this. Thanks in advance.
Cheers,
Al
--============_-1189589119==_ma============
Content-Type: text/enriched; charset="us-ascii"
Dear Python Wranglers,
Being of a scientific bent, I need to handle data in arrays to do
things like resampling and general calculations. I am used to BASIC
arrays which I can read data into from other files with no problem.
However, I am being defeated by the Python readline(s) for file import.
I can do things within Python OK, such as the example below lifted from
a help page and modified.
<fontfamily><param>Geneva</param><bigger>import array
arr=[]
for i in range(5):
arr.append([])
for j in range(10):
arr[i].append(i*j*2)
print arr[:]
s = arr [4]
a = s[1]
b = s[2]
print a-b
</bigger></fontfamily>And I can splice away and reassign values fine.
However, when I try to read in data from a file as shown below, I end
up with the lines, which I can't manipulate.
<fontfamily><param>Geneva</param><bigger>import array
arr=[]
inp = open ("dat.txt","r")
#read line into array
for line in inp.readlines():
arr.append(line)
print arr [0:3]
</bigger></fontfamily>which gives
['1 2 3\n', '4 5 6\n', '7 8 9']
as output
I have tried using the suggested
num = int(string), but I the "/n" can't be dealt with from the space
delimited simple text file I am using to test this, which is appended
to this mail.
I have consulted my Python Essential Reference, but I am failing to see
the way to deal with this. Thanks in advance.
Cheers,
Al
--============_-1189589119==_ma============--
--============_-1189589119==_============
Content-Id: <v04210102b9184a435a91@[128.135.229.16].0.0>
Content-Type: text/plain; name="dat.txt"; charset="us-ascii"
Content-Disposition: attachment; filename="dat.txt"
; modification-date="Mon, 27 May 2002 13:42:36 -0500"
1 2 3
4 5 6
7 8 9
--============_-1189589119==_============
Content-Type: text/plain; charset="us-ascii"
Alistair J. McGowan
Department of Geophysical Sciences,
University of Chicago
Chicago
IL 60637
Phone: 773-955-4040 x5-1170
Fax: 773-702-9505
"Hope is a duty from which paleontologists are exempt."
David Quammen
--============_-1189589119==_============--