Message from discussion
file data => array(s)
Received: by 10.68.190.2 with SMTP id gm2mr5754156pbc.4.1323975835700;
Thu, 15 Dec 2011 11:03:55 -0800 (PST)
Path: lh20ni26264pbb.0!nntp.google.com!news2.google.com!postnews.google.com!o7g2000yqk.googlegroups.com!not-for-mail
From: Eric <einazaki...@yahoo.com>
Newsgroups: comp.lang.python
Subject: Re: file data => array(s)
Date: Thu, 15 Dec 2011 10:37:59 -0800 (PST)
Organization: http://groups.google.com
Lines: 101
Message-ID: <388939ad-b465-4dd0-8eb8-51395ee643a7@o7g2000yqk.googlegroups.com>
References: <81b5d566-3a82-4a8e-ac8a-98b8dd92f6bc@4g2000yqu.googlegroups.com> <mailman.3658.1323903585.27778.python-list@python.org>
NNTP-Posting-Host: 128.252.125.52
Mime-Version: 1.0
X-Trace: posting.google.com 1323975835 1291 127.0.0.1 (15 Dec 2011 19:03:55 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Thu, 15 Dec 2011 19:03:55 +0000 (UTC)
Complaints-To: groups-abuse@google.com
Injection-Info: o7g2000yqk.googlegroups.com; posting-host=128.252.125.52; posting-account=5sjSAAkAAADWrvmYYN9Q5vfQ2WNdq-Wg
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/5.0 (Windows NT 5.1; rv:8.0) Gecko/20100101 Firefox/8.0,gzip(gfe)
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
On Dec 14, 4:59=A0pm, Dave Angel <d...@davea.name> wrote:
> On 12/14/2011 05:20 PM, Eric wrote:
>
>
>
>
>
>
>
> > I'm trying to read some file data into a set of arrays. =A0The file dat=
a
> > is just four columns of numbers, like so:
>
> > =A0 =A0 1.2 =A0 =A02.2 =A0 3.3 =A00.5
> > =A0 =A0 0.1 =A0 0.2 =A0 =A01.0 =A010.1
> > =A0 =A0 ... 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:
>
> > =A0 =A0 [[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:
>
> > =A0 =A0 file =3D open(fileName, 'r')
> > =A0 =A0 for line in file.readlines():
>
> The readlines() call is a waste of time/space. =A0file is already an
> iterator that'll return lines for you.
>
>
>
>
>
>
>
> > =A0 =A0 =A0 =A0d1, e1, d2, e2 =3D map(float, line.split())
> > =A0 =A0 =A0 =A0data1.append(d1) =A0# where data1, err1, data2, err2 are=
init-ed
> > as empty lists
> > =A0 =A0 =A0 =A0err1.append(e1)
> > =A0 =A0 =A0 =A0data2.append(d2)
> > =A0 =A0 =A0 =A0err2.append(e2)
> > =A0 =A0 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). =A0I've also tried
> > replacing the above "map" line with:
>
> > =A0 =A0 =A0 =A0d =3D d + map(float, line.split()) =A0# where d is initi=
alized as d
> > =3D []
>
> > 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. =A0Nothing 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(). =A0It's got some powerfu=
l
> uses when rows and columns need inverting.
>
> I didn't try it on an actual file, but the following works:
> linedata =3D =A0 =A0[[1.2, 2.2, 3.3, 0.5], [0.1, 0.2, 1.0, 10.1] ]
>
> data, err1, data2, err2 =3D zip(*linedata)
>
> print data
> print err1
> print data2
> print err2
>
> So you could try (untested)
>
> file =3D open(filename, "r")
> linedata =3D [ map(float, line) for line in file]
> data, err1, data2, err2 =3D zip(*linedata)
> file.close()
>
> DaveA
Neat. This is what I had in mind for a python-esque solution. Only
thing is "map(float,line)" should be "map(float,line.split()). Looks
like it should be easy enough to weed out any funky data sets because
between map() and zip() it's fairly picky about the amount and type of
data.
Finally, the input files I'll be using for real aren't just four
columns of data. The beginning of the file may have comments
(optional) and will have two lines of text to identify the data.
Maybe I can still do it w/o readlines.
Thanks,
eric