Tuesday, March 17, 2009

Python tip: splitting strings with whitespace

This has gotten me so many times now I figure I need to write it down. Plus, it'll be here for everyone else's benefit.

Examine the following Python code:
>>> s = "foo bar spam eggs" # two spaces between bar-spam, three between spam-eggs
>>> s.split( " " )
['foo', 'bar', '', 'spam', '', '', 'eggs']
>>> s.split( )
['foo', 'bar', 'spam', 'eggs']

Note that if you split on " " as in the first command, Python will split only on the first space in a span of whitespace. After the first, subsequent spaces are placed as empty strings into the returned list. However, if you use the split with no arguments, each split eats all connected whitespace.

No comments:

Post a Comment