[Tutor] Suggestions as to how to read a file in paragraphs

Alan Gauld alan.gauld at blueyonder.co.uk
Wed Sep 1 22:47:33 CEST 2004


> What is a more efficient way to read a file and split it into
paragraphs
> and just print those paragraphs which match a particular condition?

First, do you need the efficiency? Is it actually causing a problem
just now?

> The inefficient way is to read the whole file into one string and
then
> split it, but this becomes problematic with larger files.

You are going to have to read the whole file anyhow so this might well
be an efficient route provided you have enough RAM.

> Any suggestions on a more efficient approach or is there a module
available?

Lots of modules might help (re, parser, string, fileinput etc) but
nothing
specifically for splitting text files by paragraph. (And how do you
define a paragraph separator? Is it a blank line or is it an indented
first line? Both are valid...)

My case study topic in my tutorial does separate by blank lines but
it makes no attempt to optimise efficiency. For moderate length files
(a few hundred lines) it is adequate.


Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld/tutor2/



More information about the Tutor mailing list