[Tutor] Parsing string with variable length delimiter?

Kent Johnson kent_johnson at skillsoft.com
Tue Sep 7 18:10:26 CEST 2004


If you use string.split() with no arguments, it will treat runs of 
whitespace as a single separator. This is different behavior from 
string.split(' '), which treats each individual space as a separator:
 >>> 'a  b    c'.split()
['a', 'b', 'c']
 >>> 'a  b    c'.split(' ')
['a', '', 'b', '', '', '', 'c']

Kent

At 04:00 PM 9/7/2004 +0000, Jeffrey F. Bloss wrote:
>In general, what's the "best" way to split() a series of strings such as...
>
>TEXT      MORETEXT      SOMEMORETEXT  TEXT
>MORETEXT  TEXT          MORETEXT      SOMEMORETEXT
>
>Text field lengths are variable as are numbers of delimiter characters. 
>mystring.split(' ') yields a list including each individual space. Would 
>it be best to parse that list and reject any spaces, or is there a regex 
>way to modify the behavior of the .split() method? Or another method all 
>together?
>
>Thanks in advance!
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list