Skip to content

Instantly share code, notes, and snippets.

@saintsGrad15
Last active October 25, 2016 20:49
Show Gist options
  • Save saintsGrad15/bfd443670dba0a5945a90080f69d6f32 to your computer and use it in GitHub Desktop.
Save saintsGrad15/bfd443670dba0a5945a90080f69d6f32 to your computer and use it in GitHub Desktop.
Will split 'string' using all of the additional arguments to multisplit.
def multisplit(string, *args):
"""
Will split 'string' using all of the additional arguments to multisplit.
Will then filter out any instances of empty strings in the resultant list
For example
result = multisplit("a, b c,d e", ",", " ") # Split on both commas and spaces
result == ["a", "b", "c", "d", "e"]
:param string: (str) A string to split
:param args: An arbitrary number of
:return: (list) A list of the tokens in 'string', in order, split on all the arguments to multisplit.
"""
result = []
if len(args) > 0:
if not all(map(lambda x: isinstance(x, basestring), args)):
raise TypeError("All arguments must be instances of <basestring>.")
# Do the first split alone
result = string.split(args[0])
# Are there more characters?
if len(args) > 1:
for char in args[1:]:
iter_result = []
for token in result:
arr = token.split(char)
iter_result.extend(arr)
result = iter_result
result = filter(lambda x: x != "", result)
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment