[MATRIX-SIG] Final NumPy 1.0
Janko Hauser
jhauser@ifm.uni-kiel.de
Thu, 18 Dec 1997 19:22:20 +0100 (CET)
I understand that sys.input is something to facilitate the reading
from standard in to build command line tools and scripts which get
used in pipes.
I use currently this code, which unfortunatly needs sscanfmodule. But
it's the fastes way to read ``trusted'' data (No testing for to big,
equally formated)
def read_table(fname,header=0):
"""read_table(filename,header=number_of_header_lines)
Reads from filename number_of_fields numbers per line
to end of file. If number of num_fields per line doesn't
match, skip line"""
from sscanf import sscanf
import string
try:
fp=open(fname,'r')
except:
print "Can't open file or wrong filename in function read_table"
return
scanline='%f'
lines = fp.readlines()
n=len(string.split(lines[header]))
new_table=ones((len(lines)-header,n),typecode = Float)
while n-1:
n=n-1
scanline=scanline+' %f'
for item in xrange(len(lines)-header):
a=array(sscanf(lines[item],scanline))
new_table[item,:] = a
return(new_table)
Has someone something faster, or really sophisticated?
(sscanf is much faster with the conversion of string to float, I
tested this only with 1.4 not 1.5, where the string handling should be
faster)
__Janko
Mike Miller writes:
> >>>>> "janko" == Janko Hauser <jhauser@ifm.uni-kiel.de> writes:
> > I think there should be included some code to read and
> > write some ascii-data. Everybody has some data lying
> > around, with which he/she wants to work.
>
> There was a discussion about this on comp.lang.python a few weeks
> ago. The subject was "Proposal: sys.input()". Some code was
> posted there...
>
> Mike
_______________
MATRIX-SIG - SIG on Matrix Math for Python
send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org
_______________