reading file contents to an array (newbie)

Darren Dale dd55 at cornell.edu
Wed Jul 7 01:07:58 CEST 2004


Hello,

I am trying to learn how to read a delimited file into a numarray. I 
have a working strategy, but it doesnt seem very elegant: manually 
changing whitespace delimiters to commas, evaluating the lines to a list 
of tuples, creating a numarray object from that list.

Could I get some suggestions on how to do this more Pythonically? I have 
to read pretty large files, so this approach is probably way to slow. 
Here is my code:

from numarray import *
myFile=file('test.dat',mode='rt')
tempData=myFile.readlines()
data=[]
for line in tempData:
     line=line.replace(' ',',')
     line=line.replace('\n','')
     data.append(eval(line))
data=array(data)

Thanks...



More information about the Python-list mailing list