Splitting lines in a file

Simon Foster simon at uggs.demon.co.uk
Sun Jul 7 17:54:39 EDT 2002


Quick query, hopefully,

How come when I split a file on arbitrary whitespace I get one fewer
lines then when I split on end of line?  In the case when I split on
the \n the last line has zero length.

C:\hexbin>python
Python 2.2 (#28, Dec 21 2001, 12:21:22) [MSC 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> f = file( 'siu_build.hex' ).read()
>>> len( f.split() )
1254
>>> len( f.split( '\n' ))
1255
>>> lines = f.split( '\n' )
>>> len( lines[-1] )
0
>>>



More information about the Python-list mailing list