issues simply parsing a whitespace-delimited textfile in python script

Damon Getsman dgetsman at amirehab.net
Wed May 21 12:47:57 EDT 2008


On May 21, 11:15 am, Paul McGuire <pt... at austin.rr.com> wrote:
> <snip>
>
>     for cur_rec in Lastdump.readline():
>
> is the problem.  readline() returns a string containing the next
> line's worth of text, NOT an iterator over all the subsequent lines in
> the file.  So your code is really saying:
>
>     next_line_in_file = Lastdump.readline():
>     for cur_rec in next_line_in_file:
>
> which of course, is iterating over a string character by character.
>
> Since you are opening Lastdump (not great casing for a variable name,
> BTW - looks like a class name with that leading capital letter), it
> gives you an iterator already.  Try this instead:
>
> lastdump = open('/tmp/esd_tmp', 'r')
>
> cur_rec = lastdump.next()
>
> ...
>
>     for cur_rec in lastdump:
>
> ...
>
> This should get you over the hump on reading the file.
>
> Also, may I suggest this method for splitting up each record line, and
> assigning individual fields to variables:
>
>     user,s1,s2,day,month,date,time,desc = cur_rec.split(None,7)
>
> -- Paul

Well the individual variables isn't exactly appropriate as I'm only
going to be using 2 of the fields.  I think I will set those to
individual variables with a slice of what you mentioned, though, for
readability.  Thank you for the tips, they were all much appreciated.

-Damon



More information about the Python-list mailing list