Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #17239
| Date | 2011-12-14 17:59 -0500 |
|---|---|
| From | Dave Angel <d@davea.name> |
| Subject | Re: file data => array(s) |
| References | <81b5d566-3a82-4a8e-ac8a-98b8dd92f6bc@4g2000yqu.googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3658.1323903585.27778.python-list@python.org> (permalink) |
On 12/14/2011 05:20 PM, Eric wrote: > 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(): The readlines() call is a waste of time/space. file is already an iterator that'll return lines for you. > 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 When I see a problem like this, I turn to zip(). It's got some powerful uses when rows and columns need inverting. I didn't try it on an actual file, but the following works: linedata = [[1.2, 2.2, 3.3, 0.5], [0.1, 0.2, 1.0, 10.1] ] data, err1, data2, err2 = zip(*linedata) print data print err1 print data2 print err2 So you could try (untested) file = open(filename, "r") linedata = [ map(float, line) for line in file] data, err1, data2, err2 = zip(*linedata) file.close() Note that your code won't work (and mine probably won't either) if one of the lines has 3 or 5 items. Or if one of the numbers isn't legal format for a float. So you need to think about error checking, or decide whether a partial result is important. -- DaveA
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
file data => array(s) Eric <einazaki668@yahoo.com> - 2011-12-14 14:20 -0800
Re: file data => array(s) Dave Angel <d@davea.name> - 2011-12-14 17:59 -0500
Re: file data => array(s) Eric <einazaki668@yahoo.com> - 2011-12-14 15:25 -0800
Re: file data => array(s) Eric <einazaki668@yahoo.com> - 2011-12-15 10:37 -0800
Re: file data => array(s) MRAB <python@mrabarnett.plus.com> - 2011-12-15 19:34 +0000
Re: file data => array(s) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-14 23:27 +0000
Re: file data => array(s) Eric <einazaki668@yahoo.com> - 2011-12-15 10:51 -0800
csiph-web