File reading using delimiters

Peter Hansen peter at engcorp.com
Mon Jun 9 13:05:56 EDT 2003


Kylotan wrote:
> 
> All the examples of reading files in Python seem to concern reading a
> line at a time. But this is not much good to me as I want to be able
> to read up to arbitrary delimiters without worrying about how many
> lines I'm spanning. With my rudimentary Python knowledge I'm having to
> read in multiple lines, concatenate them, search for the delimiter,
> split the result if necessary, and carry forward whatever was after
> the delimiter to the next operation. Is there a better way of reading
> until a certain character is encountered, and no more?

You could always avoid readline() and use just read() to get either
all the data (for files under a few tens of megabytes) or read(x) 
where x is some block-size, for larger files.  Without more information
about the nature of the files and the type of scanning, I can't
help more.

-Peter




More information about the Python-list mailing list