[Tutor] File IO Help again

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Thu Oct 27 21:57:12 CEST 2005



On Thu, 27 Oct 2005, Mike Haft wrote:

> why can I not get the readline() or readlines() functions to work, I'm
> told they are not defined whenever I try.

Don't paraphrase.  *grin*

Show us what you did in terms of the code that you ran.  Also, include the
literal error message and its traceback.  This is important because we
need to make sure we can reproduce whatever problem you're running into.



> Can I just use a for loop type of construction and substitute an x
> variable where there is curently a [:1] and a "x" i.e.:
>
> if line[:y] == x:

Yes, although you might be able to simplify this; doing a loop here seems
like too much work.  Depending on how your lines are structured, it might
even be possible to do something like:

    pieces = line.split()
    if len(pieces) > 0 and pieces[0] == x:
        ...


>From the data you've shown us:

> MONTH  RAIN   AVTEMP  RAD  EVAP
> ****************************************
> 1      12.4    12.0    *   10
> 2      13.9    30.0    *   11

it seems that using a split to break the line into columns should help
simplify the problem.



More information about the Tutor mailing list