Split string by multiple separators?
Question: > **Possible Duplicate:**  
>  [Python: Split string with multiple
> delimiters](http://stackoverflow.com/questions/4998629/python-split-string-
> with-multiple-delimiters)

Can I do something similar in Python?

Split method in VB.net:

    
    
    Dim line As String = "Tech ID: xxxxxxxxxx Name: DOE, JOHN Account #: xxxxxxxx"
    Dim separators() As String = {"Tech ID:", "Name:", "Account #:"}
    Dim result() As String
    result = line.Split(separators, StringSplitOptions.RemoveEmptyEntries)
    


Answer: Given a bad data format like this, you could try `re.split()`:

    
    
    >>> import re
    >>> mystring = "Field 1: Data 1 Field 2: Data 2 Field 3: Data 3"
    >>> a = re.split(r"(Field 1:|Field 2:|Field 3:)",mystring)
    ['', 'Field 1:', ' Data 1 ', 'Field 2:', ' Data 2 ', 'Field 3:', ' Data 3']
    

Your job would be much easier if the data was sanely formatted, with quoted
strings and comma-separated records. This would admit the use of the `csv`
module for parsing of comma-separated value files.

Edit:

You can filter out the blank entries with a list comprehension.

    
    
    >>> a_non_empty = [s for s in a if s]
    >>> a_non_empty
    ['Field 1:', ' Data 1 ', 'Field 2:', ' Data 2 ', 'Field 3:', ' Data 3']
    

