howto split string with both comma and semicolon delimiters

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dmitrey

    howto split string with both comma and semicolon delimiters

    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.
  • Gary Herron

    #2
    Re: howto split string with both comma and semicolon delimiters

    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.
    --

    >
    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


    Comment

    • Daniel Fetchinson

      #3
      Re: howto split string with both comma and semicolon delimiters

      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

      Comment

      • bvdp

        #4
        Re: howto split string with both comma and semicolon delimiters

        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(",")

        Comment

        • MRAB

          #5
          Re: howto split string with both comma and semicolon delimiters

          On Jun 12, 8:06 pm, bvdp <b...@mellowood .cawrote:
          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((', ', ';')).

          Comment

          Working...