E
Eric
I'm trying to read some file data into a set of arrays. The file data
is just four columns of numbers, like so:
1.2 2.2 3.3 0.5
0.1 0.2 1.0 10.1
... and so on
I'd like to read this into four arrays, one array for each column.
Alternatively, I guess something like this is okay too:
[[1.2, 2.2, 3.3, 0.5], [0.1, 0.2, 1.0, 10.1], ... and so on]
I came up with the following for the four array option:
file = open(fileName, 'r')
for line in file.readlines():
d1, e1, d2, e2 = map(float, line.split())
data1.append(d1) # where data1, err1, data2, err2 are init-ed
as empty lists
err1.append(e1)
data2.append(d2)
err2.append(e2)
file.close()
But somehow it doesn't seem very python-esque (I'm thinking there's a
more elegant and succinct way to do it in python). I've also tried
replacing the above "map" line with:
d = d + map(float, line.split()) # where d is initialized as d
= []
But all I get is one long flat list, not what I want.
So is the map and append method the best I can do or is there a
slicker way?
One more thing, no numpy. Nothing against numpy but I'm curious to
see what can be done with just the box stock python install.
TIA,
eric
is just four columns of numbers, like so:
1.2 2.2 3.3 0.5
0.1 0.2 1.0 10.1
... and so on
I'd like to read this into four arrays, one array for each column.
Alternatively, I guess something like this is okay too:
[[1.2, 2.2, 3.3, 0.5], [0.1, 0.2, 1.0, 10.1], ... and so on]
I came up with the following for the four array option:
file = open(fileName, 'r')
for line in file.readlines():
d1, e1, d2, e2 = map(float, line.split())
data1.append(d1) # where data1, err1, data2, err2 are init-ed
as empty lists
err1.append(e1)
data2.append(d2)
err2.append(e2)
file.close()
But somehow it doesn't seem very python-esque (I'm thinking there's a
more elegant and succinct way to do it in python). I've also tried
replacing the above "map" line with:
d = d + map(float, line.split()) # where d is initialized as d
= []
But all I get is one long flat list, not what I want.
So is the map and append method the best I can do or is there a
slicker way?
One more thing, no numpy. Nothing against numpy but I'm curious to
see what can be done with just the box stock python install.
TIA,
eric