Splitting lines in a file

William Park opengeometry at NOSPAM.yahoo.ca
Sun Jul 7 18:30:55 EDT 2002


Simon Foster <simon at uggs.demon.co.uk> wrote:
> On 8 Jul 2002 00:07:47 +0200, Chris Liechti <cliechti at gmx.net> wrote:
> 
>>'cause split() drops the empty part at the end:
>>>>> '1\n2\n3\n'.split()
>>['1', '2', '3']
>>>>> '1\n2\n3\n'.split('\n')
>>['1', '2', '3', '']
>>
> 
> 
> I see it, but I don't see why?  What is the explanation for this?  It
> seems at odds with the documentation.

'1\n2\n3\n'.split() works as follows:
    1\n2\n3\n -> 1\n2\n3 -> 1 \n 2 \n 3 -> 1 2 3

'1\n2\n3\n'.split('\n') works as follows:
    1\n2\n3\n -> 1 \n 2 \n 3 \n -> 1 2 3 ''
where the last '\n' separates '3' and '' (null).

-- 
William Park, Open Geometry Consulting, <opengeometry at yahoo.ca>
8-CPU Cluster, Hosting, NAS, Linux, LaTeX, python, vim, mutt, tin



More information about the Python-list mailing list