Re: newbie would appreciate input examples

Steven D. Majewski (sdm7g@elvis.med.virginia.edu)
Fri, 5 Aug 1994 12:36:24 -0400

On Aug 5, 16:41, Guido.van.Rossum@cwi.nl wrote:
>
> > 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.

Oops!

This bug is due to the fact that what I first wrote ( and
actually tested ) was the more general split on whitespace
case:

fields = string.split( fp.readline() )

and then I changed it to splitfields without checking!

string.split( ' ' ) or string.split( '' )

return []

while string.splitfields( '', '\t' ) returns ['']

A difference I hadn't noticed before, but is (I think) necessary
to maintain the reversability of string.splitfields and
string.joinfields. ( Which is a quite useful feature! )

- Steve "Code in haste, repend in leisure!" Majewski