Created
February 10, 2013 14:06
-
-
Save hackjoy/4749678 to your computer and use it in GitHub Desktop.
Split a string based on pre defined split characters
This file contains 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
def split_string(source,splitlist): | |
output = [] | |
atsplit = True | |
for char in source: | |
if char in splitlist: | |
atsplit = True | |
else: | |
if atsplit: | |
output.append(char) | |
atsplit = False | |
else: | |
output[-1] = output[-1] + char | |
return output | |
out = split_string("This is a test-of the,string separation-code!"," ,!-") | |
print out | |
#>>> ['This', 'is', 'a', 'test', 'of', 'the', 'string', 'separation', 'code'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment