Created
August 19, 2012 01:23
-
-
Save zthomae/3390778 to your computer and use it in GitHub Desktop.
Python snippets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## SPLIT AROUND SPACE ## | |
| import string | |
| # All forms of whitespace except the space | |
| whitespace = string.whitespace[:-1] | |
| # Split a string around its whitespace, with words and whitespace being entries in a list | |
| def split_around_space(string): | |
| new = [] | |
| current = "" | |
| for i in string: | |
| if i.isspace(): | |
| new.append(current) | |
| current = "" | |
| if i in whitespace: # Included for demonstration: Only include "important" whitespace (from above) | |
| new.append(i) | |
| else: | |
| current += i | |
| new.append(current) | |
| return new |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment