Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion file data => array(s)

Received: by 10.68.189.233 with SMTP id gl9mr3233225pbc.8.1323903586218;
        Wed, 14 Dec 2011 14:59:46 -0800 (PST)
Path: lh20ni23080pbb.0!nntp.google.com!news1.google.com!goblin1!goblin.stu.neva.ru!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path: <d...@davea.name>
X-Original-To: python-l...@python.org
Delivered-To: python-l...@mail.python.org
X-Spam-Status: OK 0.001
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python': 0.08; "(i'm":
	0.09; 'append': 0.09; 'either)': 0.09; 'initialized': 0.09;
	'okay': 0.09; 'rows': 0.09; 'tia,': 0.09; 'subject:file': 0.12;
	'"r")': 0.16; "'r')": 0.16; 'arrays,': 0.16; 'arrays.': 0.16;
	'columns': 0.16; 'd1,': 0.16; 'd2,': 0.16; 'data2': 0.16;
	'file.close()': 0.16; 'install.': 0.16; 'iterator': 0.16; 'numpy':
	0.16; 'slicker': 0.16; 'subject:array': 0.16; 'succinct': 0.16;
	"that'll": 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.18;
	'2.2': 0.18; 'file,': 0.19; 'cc:no real name:2**0': 0.20;
	'trying': 0.21; 'subject:data': 0.21; "doesn't": 0.22; 'header:In-
	Reply-To:1': 0.22; '0.1': 0.23; 'items.': 0.23; 'replacing': 0.23;
	'somehow': 0.23; 'way?': 0.23; 'cc:2**0': 0.24; 'code': 0.25;
	'guess': 0.26; "i'm": 0.26; 'tried': 0.27; '(and': 0.28; 'mine':
	0.28; 'lists': 0.28; 'partial': 0.29; 'problem': 0.29; 'print':
	0.29; 'cc:addr:python.org': 0.29; 'error': 0.29; 'pm,': 0.29;
	'lines': 0.30; 'array': 0.30; 'seem': 0.30; 'float.': 0.30;
	'option:': 0.30; "i've": 0.31; "didn't": 0.31; 'actual': 0.32;
	"isn't": 0.33; "won't": 0.33; 'header:User-Agent:1': 0.33;
	'there': 0.33; 'decide': 0.33; 'done': 0.34; 'probably': 0.34;
	'eric': 0.34; 'curious': 0.34; 'elegant': 0.34; 'something': 0.35;
	'file': 0.36; '...': 0.36; 'uses': 0.36; 'but': 0.37; 'list,':
	0.37; 'received:192': 0.37; "there's": 0.37; 'think': 0.37;
	'could': 0.37; 'some': 0.38; "i'd": 0.39; 'data,': 0.39; "it's":
	0.40; 'received:192.168': 0.40; 'more': 0.61; 'your': 0.61;
	'you.': 0.63; 'legal': 0.70; 'header:Reply-To:1': 0.71; 'reply-
	to:no real name:2**0': 0.72; 'to:addr:yahoo.com': 0.83; '05:20':
	0.84; '10.1': 0.84; 'so:': 0.84; '3.3': 0.91
Date: Wed, 14 Dec 2011 17:59:25 -0500
From: Dave Angel <d...@davea.name>
User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US;
	rv:1.9.2.23) Gecko/20110922 Thunderbird/3.1.15
MIME-Version: 1.0
To: Eric <einazaki...@yahoo.com>
Subject: Re: file data => array(s)
References: <81b5d566-3a82-4a8e-ac8a-98b8dd92f...@4g2000yqu.googlegroups.com>
In-Reply-To: <81b5d566-3a82-4a8e-ac8a-98b8dd92f...@4g2000yqu.googlegroups.com>
X-Provags-ID: V02:K0:u+bMnKd5IeabM7saz2Mfeou+VPOEuKh/e8BBK8B3lCc
	/CWVftXWqWdxtLRE0dH8oj8KBtwhA5e7jcVbLWZye4HLsNiF72
	m5zmOLy8Fa78DgU4AEHIzslv7sMiFRqhWP/MJpyHt6fEju6AIy
	bUeZmhkgoaqj3E/nhAlrmcuSuK9SsIRkR2dD2dm9qH/hwepWm9
	BdSsMOAzfKpMeoQjSLB4abCBnFkneTEKxG8Gl+3DS7eE6UqmUM
	Jl/omNLu88M9qaZavVAd8vRnkxGKubrvlann2GnOT1eGTSFgOp
	dq6Z1F17ryfLHpbc/4eSbXYiwi+fA4dwWnW8CCUmEuT7UUsOUm
	3GZvKkCEvqQo0lrG3GXM=
Cc: python-l...@python.org
X-BeenThere: python-l...@python.org
X-Mailman-Version: 2.1.12
Precedence: list
Reply-To: d...@davea.name
List-Id: General discussion list for the Python programming language
	<python-list.python.org>
List-Unsubscribe: <http://mail.python.org/mailman/options/python-list>,
	<mailto:python-list-requ...@python.org?subject=unsubscribe>
List-Archive: <http://mail.python.org/pipermail/python-list>
List-Post: <mailto:python-l...@python.org>
List-Help: <mailto:python-list-requ...@python.org?subject=help>
List-Subscribe: <http://mail.python.org/mailman/listinfo/python-list>,
	<mailto:python-list-requ...@python.org?subject=subscribe>
Newsgroups: comp.lang.python
Message-ID: <mailman.3658.1323903585.27778.python-l...@python.org>
Lines: 73
NNTP-Posting-Host: 2001:888:2000:d::a6
X-Trace: 1323903585 news.xs4all.nl 6861 [2001:888:2000:d::a6]:46345
X-Complaints-To: ab...@xs4all.nl
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

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