split() and string.whitespace

Scott David Daniels Scott.Daniels at Acm.Org
Mon Nov 3 10:51:24 EST 2008


Steven D'Aprano wrote:
> 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()
Or, for faster per-repetition (blending in to your use-case):

     import string
     SEP = string.maketrans('abc \t', '     ')
     ...
     parts = 'whatever, abalone dudes'.translate(SEP).split()
     print parts

['wh', 'tever,', 'lone', 'dudes']


--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list