[Tutor] reading data

Alan Gauld alan.gauld at btinternet.com
Fri Jul 24 01:13:13 CEST 2009


"chris Hynes" <cjhynes36 at hotmail.com> wrote

Its not a good idea to start a new thread by replying to an existing one.
Threaded newsreaders or mailing tools will still see it as part of the
old thread and bored readers may not notice the new subject...

However....

> have a data file in which the first line is made up of words. Here is the 
> original data file:
>
> #Column density-scaled with production rate 3.16227766016838e+25
> -10.0000 0.000e+00  0.000e+00 0.000e+00 0.000e+00 0.000e+00 0.000e+00
> ...

> I'd like to my program to skip any lines that begin with words
> and move on to lines that contain numbers.

We've had a few requests very similar to this over the last couple of 
weeks.

the isdigit() method of strings may well help here.

----------------------
from numpy import *
row=0
columnindex=0
x=open('halfmethanol.col','rt')
for line in x.readlines():
    data=line.split()
    columnindex=len(data)
    row=row+1

The row line does nothing and can be deleted.
But it looks like a hard wayto find the length of the last line.
Is that really what you want?

If so you could gather the data wjile you are at it:

data = [line.split() for line in x.readlines()]
columnindex = len(x[-1])

Then the next loop can be over x rather than rereading the file
and splitting the lines again. And if you use enumerate you can
miss out the incrementing here too:

temp=ones((row,columnindex))
for row, line in enumerate(x):
    for column in range(columnindex):
        temp[row,column]=data[column]

However I'm still not totally clear what you are achieving with this?


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/ 




More information about the Tutor mailing list