howto split string with both comma and semicolon delimiters

Discussion in 'Python' started by dmitrey, Jun 12, 2008.

  1. dmitrey

    dmitrey Guest

    hi all,
    howto split string with both comma and semicolon delimiters?

    i.e. (for example) get ['a','b','c'] from string "a,b;c"

    I have tried s.split(',;') but it don't work
    Thx, D.
     
    dmitrey, Jun 12, 2008
    #1
    1. Advertising

  2. dmitrey

    Gary Herron Guest

    dmitrey wrote:
    > hi all,
    > howto split string with both comma and semicolon delimiters?
    >
    > i.e. (for example) get ['a','b','c'] from string "a,b;c"
    >
    > I have tried s.split(',;') but it don't work
    > Thx, D.
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >


    The regular expression module has a split function that does what you
    want.

    >>> import re
    >>> r =',|;' # or this also works: '[,;]'
    >>> s = "a,b;c"
    >>> re.split(r,s)

    ['a', 'b', 'c']


    Gary Herron
     
    Gary Herron, Jun 12, 2008
    #2
    1. Advertising

  3. > howto split string with both comma and semicolon delimiters?
    >
    > i.e. (for example) get ['a','b','c'] from string "a,b;c"
    >
    > I have tried s.split(',;') but it don't work


    A very pedestrian solution would be:

    def multisplit( s, seps ):

    words = [ ]
    word = ''
    for char in s:
    if char in seps:
    if word:
    words.append( word )
    word = ''
    else:
    word += char

    if word:
    words.append( word )

    return words


    Cheers,
    Daniel
    --
    Psss, psss, put it down! - http://www.cafepress.com/putitdown
     
    Daniel Fetchinson, Jun 12, 2008
    #3
  4. dmitrey

    bvdp Guest

    dmitrey wrote:
    > hi all,
    > howto split string with both comma and semicolon delimiters?
    >
    > i.e. (for example) get ['a','b','c'] from string "a,b;c"
    >
    > I have tried s.split(',;') but it don't work
    > Thx, D.


    Howabout:

    s = s.replace(";", ",")
    s = s.split(",")
     
    bvdp, Jun 12, 2008
    #4
  5. dmitrey

    MRAB Guest

    On Jun 12, 8:06 pm, bvdp <> wrote:
    > dmitrey wrote:
    > > hi all,
    > > howto split string with both comma and semicolon delimiters?

    >
    > > i.e. (for example) get ['a','b','c'] from string "a,b;c"

    >
    > > I have tried s.split(',;') but it don't work
    > > Thx, D.

    >
    > Howabout:
    >
    > s = s.replace(";", ",")
    > s = s.split(",")


    I've wondered in the past whether there would be sufficient need for
    things like s.split((',', ';')) and s.partition((',', ';')).
     
    MRAB, Jun 13, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MattyWix
    Replies:
    7
    Views:
    688
    Thad Smith
    Sep 17, 2006
  2. Gunter Hansen
    Replies:
    5
    Views:
    935
    Roedy Green
    Sep 1, 2011
  3. Albert Schlef

    String#split and capturing delimiters

    Albert Schlef, Oct 30, 2009, in forum: Ruby
    Replies:
    3
    Views:
    185
    Rajinder Yadav
    Oct 30, 2009
  4. Replies:
    11
    Views:
    575
    James Taylor
    Jul 29, 2005
  5. Peter Michaux

    Cookies: semicolon vs. semicolon-space

    Peter Michaux, Dec 10, 2006, in forum: Javascript
    Replies:
    3
    Views:
    221
    Michael Winter
    Dec 11, 2006
Loading...

Share This Page