Re: newbie would appreciate input examples

Guido.van.Rossum@cwi.nl
Fri, 05 Aug 1994 16:41:18 +0200

> import string
> fp = open( filename, "r" )
> while 'True' :
> fields = string.splitfields( fp.readline(), '\t' )
> if not fields : break
> process( fields[0], fields[1:] )

Unfortunately, this version will loop forever at EOF since
string.splitfields('', '\t') returns [''] which is not false.
Therefore you must use Steven's second solution:

> line = fp.readline()
> if not line : break
> fields = string.splitfields( line, '\t' )

If you want to stop at EOF or a blank line, you could try

if not fields [1:] : break

--Guido van Rossum, CWI, Amsterdam <Guido.van.Rossum@cwi.nl>
<URL:http://www.cwi.nl/cwi/people/Guido.van.Rossum.html>