Newbie Import two coloums of floating point data into python HOWTO

John Gorman jdgorman at ieee.org
Wed Mar 13 03:06:05 EST 2002


Andy,
    There are also several python scripts/utilities that people have 
posted for loading tabular data into python and numpy arrays. These 
might be overkill for what you're doing, but could be helpful for large 
arrays.

    Check out the following:
ASV, csv, DataHandlers (see Parnassus link below)
http://www.vex.net/parnassus/apyllo.py/973100124

or TableIO
http://php.iupui.edu/~mmiller3/python/

TableIO has hooks to convert ascii/csv data into Numerical Python 
arrays. This is quite useful and I've used it in the past to migrate 
arrays between Matlab, NumPy, Gnumeric and <gasp> MS Excel.  This'll 
even work for importing ascii formatted images into Python, although I 
wouldn't recommend it for that purpose ;^).

Cheers,
John Gorman


Andy Gimblett wrote:

> On Mon, Mar 11, 2002 at 04:22:48AM -0800, Andybee wrote:
> 
> 
>>Can some one tell me how to import two coloumns of data in a ascii
>>text file,into two arrays in python? I cant seem to work it out. The
>>data is genereated in matlab and I want to import it into python
>>
> 
> Let's say you data looks something like this:
> 
> 1.0   5.0
> 2.0   10.2
> 3.1   13.4
> 
> etc.
> 
> Then here's one answer...  There are probably cleverer and more
> efficient ways of doing this, but this one's pretty easy to understand
> and so hopefully more suited to a newbie... :-)
> 
> I'm also not _quite_ answering your question, because you asked how to
> get two seperate lists, one per column, whereas what this does is
> return a list of tuples (one tuple per data line), which is how _I'd_
> do it.  ;-) Easy to change to your way, anyway.
> 
> def foobar(filename):
> 
>     # We'll return a single list, consisting of 2-element tuples, one
>     # per line in the data file.
> 
>     results = []
> 
>     # Open the input file and read the lines.  Note that this reads
>     # them all in before continuing, and so would be unsuitable for a
>     # large file.  Possible alternatives would involve using
>     # readline() or xreadlines(), the investigation of which is left
>     # as an exercise to the reader.  :-)
>     
>     input = open(filename)
>     lines = input.readlines()
> 
>     # Now iterate over the lines, populating the result
>     
>     for line in lines:
>         
>         if not line.strip():
>             # Skip blank line
>             continue
> 
>         # Split the line up, convert the values to floats, add it to
>         # the result set.
>         
>         (first, second) = line.split()
>         row = (float(first), float(second))
>         results.append(row)
> 
>     return results
> 
> BTW, if you haven't seen it yet, I'd heartily recommend that you work
> through the python tutorial at:
> 
>     http://www.python.org/doc/current/tut/tut.html
> 
> This contains everything you'd need to answer your question/understand
> the above, and a whole lot more.
> 
> HTH,
> 
> Andy
> 
> 




More information about the Python-list mailing list