split() and string.whitespace

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Fri Oct 31 20:23:47 EDT 2008


On Fri, 31 Oct 2008 12:18:32 -0700, Chaim Krause wrote:

> I have arrived here while attempting to break down a larger problem. I
> got to this question when attempting to split a line on any whitespace
> character so that I could then add several other characters like ';' and
> ':'. Ultimately splitting a line on any char in a union of
> string.whitespace and some pre-designated chars.
> 
> I am now beginning to think that I have outgrown split() and must move
> up to regular expressions. If that is the case, I will go off and RTFM
> on RegEx.

Or just do this:

s = "the quick    brown\tdog\njumps over\r\n\t the lazy dog"
s = s.replace('\t', ' ').replace('\n', ' ').replace('\r', ' ')
s.split(' ')


or even simpler:

s.split()


-- 
Steven



More information about the Python-list mailing list