Newbie question about file input

Ben Last ben at benlast.com
Tue Aug 17 11:46:28 EDT 2004


split() delimits on sequences of whitespace characters, not just spaces, so:
>>>"\n".split()
[]
>>>"".split()
[]
>>>
...both give the same result; an empty list.

>From the documentation:
split( s[, sep[, maxsplit]])

Return a list of the words of the string s. If the optional second argument
sep is absent or None, the words are separated by arbitrary strings of
whitespace characters (space, tab, newline, return, formfeed). If the second
argument sep is present and not None, it specifies a string to be used as
the word separator. The returned list will then have one more item than the
number of non-overlapping occurrences of the separator in the string. The
optional third argument maxsplit defaults to 0. If it is nonzero, at most
maxsplit number of splits occur, and the remainder of the string is returned
as the final element of the list (thus, the list will have at most
maxsplit+1 elements).

regards
b

> -----Original Message-----
> From: python-list-bounces+ben=benlast.com at python.org
> [mailto:python-list-bounces+ben=benlast.com at python.org]On Behalf Of
> Aaron Deskins
> Sent: 17 August 2004 16:08
> To: python-list at python.org
> Subject: Re: Newbie question about file input
>
>
> Christopher T King wrote:
>
> > On Tue, 17 Aug 2004, Aaron Deskins wrote:
> >
> >
> >>    Also, what exactly is stored when a blank line is read by the
> >>readline command? A zero, blank, or what???
> >
> >
> > readline() always includes the '\n' that terminates the line, so any
> > 'blank' line that is read will be returned as a simply '\n'.  The only
> > time readline() won't return a line terminated with '\n' is if the last
> > line of the file isn't terminated with '\n' (common on
> Windows), in which
> > context a blank line is meaningless.
> >
> So why does a linesplit fail after the program reads a blank line (or a
> '\n')? Is this simply because there's no way to assign the variable '\n' ?
>
> --
> Aaron Deskins
> Graduate Student
> Chemical Engineering
> Purdue University
> --
> http://mail.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list