[Tutor] Simple text file processing using fileinput module. "Grabbing successive lines" failure

David Rock david at graniteweb.com
Mon Jul 2 19:39:50 CEST 2012


* Flynn, Stephen (L & P - IT) <Steve.Flynn at capita.co.uk> [2012-07-02 15:03]:
> Tutors,
> 
> Can someone explain to me how I am supposed to make use of readline()
> to grab the next line of a text file please? It may be that I should
> be using some other module, but chose fileinput as I was hoping to
> make the little routine as generic as possible; able to spot short
> lines in tab separated, comma separated, pipe separated, ^~~^
> separated and anything else which my clients feel like sending me.

There are a couple issues that you need to resolve.  For starters, there
is no guarantee that the successive line is actually part of the
preceding line.  It could very well be that the original line is simply
truncated, in which case trying to append the following line would be
incorrect.

What I typically do in a case like this is use a flag variable and pull
the offending line(s).  So, you need to first determine the best course
of action for resolving the inconsistency (eg, how do you verify the
following line belongs with the preceding)?

Try checking the line, if it's less than 13 then flag and store in a
buffer and continue.  The following line _should_ also error, in which
case, you can try to resolve the two lines, or fail out if the criteria
isn't met.  

Essentially, your problem isn't with using fileinput, it's with how you
handle each line that comes in.

-- 
David Rock
david at graniteweb.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 190 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/tutor/attachments/20120702/f20d580d/attachment-0001.pgp>


More information about the Tutor mailing list