faqts : Computers : Programming : Languages : Python : Snippets : Strings

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

12 of 17 people (71%) answered Yes
Recently 6 of 10 people (60%) answered Yes

Entry

String split

Aug 9th, 2008 02:21
Sek Tea, dunk fordyce, Douwe Osinga, Nathan Wallace, unknown unknown, Hans Nowak, Snippet 74, Martijn Faassen


"""
Packages: basic_datatypes.strings
"""
""" 
> I'm trying to split a string on a number of different characters into 
a
> list of separate strings using split in the string library. So far
> example I want it to split when it sees a space, a period (.), a comma
> and so on.
> 
> Is there an easy way to do this?
If I understand what you mean, then this function should do what you
want:
"""
from string import split
def multisplit(s, sepchars):
    """Split a string on multiple characters.
    s        - string to split
    sepchars - a string with characters to split on, or a list of
characters
    Returns  - the splitted string
    NOTE: A regular expression is probably better. :)
    """
    # split it up with the first char to split for
    splitted = split(s, sepchars[0])
    # now split each element for each other char
    for c in sepchars[1:]:
        # everything will end up in newsplitted
        newsplitted = []
        # for each element in the list we want to split,
        # split it for char c, and put the results in new list
        for element in splitted:
            newsplitted = newsplitted + split(element, c)
        # now we want to split this big list
        splitted = newsplitted
    return splitted
"""
Note that this will return empty strings in the list when it encounters 
a
combination of split characters after each other. (if you for instance
want to split on '.' and ',', the combination '.,' in the string will
generate an empty string in the list.
Also note that it's probably better/faster (but for me, not easier) to 
use
regular expressions (module 're') for things like this, but I'll leave
this to the regular expression fanatics. :)
Studying regular expressions is not a bad idea though if you encounter
string processing problems a lot.
"""
Douwe:
Isn't this a bit complicated, considering what you're trying to achieve?
Why not replace all occurences of sepchars in s by sepchars[0] and then 
split on sepchars[0]? ie:
def multisplit(s, sepchars):
    """Split a string on multiple characters.
    s        - string to split
    sepchars - a string with characters to split on, or a list of
characters
    Returns  - the splitted string
    NOTE: A regular expression is probably better. :)
    """
    for sc in sepchars[1:]:
        s=s.replace(sc, sepchars[0])
    return s.split(sepchars[0])
http://regalos-de-navidad.blogspot.com/
http://regalosdesanvalentin.blogspot.com/
http://ideas-para-regalar.blogspot.com/