Quote aware split

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

    Quote aware split

    Hi,

    After trawling through the archives for a simple quote aware split
    implementation (ie string.split-alike that only splits outside of
    matching quote) and coming up short, I implemented a quick and dirty
    function that suits my purposes.

    It's ugly and it doesn't use a stack, it only supports a single
    character as a 'sep' function, only supports one type of quote (ie '
    or " but not both), but it does the job, and since there have been a
    few appeals over the years for something of this sort I have decided
    to post what I have:

    --- BEGIN ---
    #!/usr/bin/env python

    def qsplit(chars, sep, quote="'"):
    """ Quote aware split """
    qcount = 0
    splitpoints = [-1] # ie. seperator char found before first letter ;)
    for index, c in enumerate(chars ):
    if c is quote:
    qcount += 1
    if c is sep and qcount % 2 == 0:
    splitpoints.app end(index)

    # slice chars by splitpoints *omitting the separator*
    slices = [chars[splitpoints[i]+1:splitpoints[i+1]]
    for i in range(len(split points)-1)]

    # last slice will be of the form chars[last:] which we couldnt do above
    slices.append(c hars[splitpoints[-1]+1:])
    return slices


    if __name__ == "__main__":
    test = "This is gonna be in quotes ';' and this is not; lets see
    how we split"

    test2 = """
    A more complex example; try this on for size:

    create function blah '
    split me once;
    split me twice; '
    end;
    'one more time;'
    and again;
    """
    print "*--split--*".join(qsplit( test, ';'))
    print "*--split--*".join(qsplit( test2, ';'))

    # vim:tabstop=4:s hiftwidth=4:exp andtab
    --- END ---

    Regards,
    Ondrej Baudys
  • Diez B. Roggisch

    #2
    Re: Quote aware split

    Ondrej Baudys wrote:
    Hi,
    >
    After trawling through the archives for a simple quote aware split
    implementation (ie string.split-alike that only splits outside of
    matching quote) and coming up short, I implemented a quick and dirty
    function that suits my purposes.
    <snip/>

    Maybe using the csv module together with cStringIO would be more
    straightforward .

    Diez

    Comment

    • John Machin

      #3
      Re: Quote aware split

      On May 16, 10:50 am, "Ondrej Baudys" <obau...@gmail. comwrote:
      # last slice will be of the form chars[last:] which we couldnt do above
      Who are "we"? Here's another version with the "couldn't do" problem
      fixed and a few minor enhancements:

      def qsplit2(chars, sep=",", quote="'"):
      """ Quote aware split """
      assert sep != quote
      can_split = True
      splitpoints = [-1] # ie. separator char found before first
      letter ;)
      for index, c in enumerate(chars ):
      if c == quote:
      can_split = not can_split
      elif c == sep and can_split:
      splitpoints.app end(index)
      if not can_split:
      raise ValueError("Unt erminated quote")
      splitpoints.app end(len(chars))
      # slice chars by splitpoints *omitting the separator*
      slices = [chars[splitpoints[i]+1:splitpoints[i+1]]
      for i in range(len(split points)-1)]
      return slices

      Cheers,
      John

      Comment

      • Tim Arnold

        #4
        Re: Quote aware split

        "Ondrej Baudys" <obaudys@gmail. comwrote in message
        news:mailman.77 21.1179276620.3 2031.python-list@python.org ...
        Hi,
        >
        After trawling through the archives for a simple quote aware split
        implementation (ie string.split-alike that only splits outside of
        matching quote) and coming up short, I implemented a quick and dirty
        function that suits my purposes.
        Take a look at pyparsing--you'll like it I think.
        esp. http://pyparsing.wikispaces.com/Examples

        --Tim Arnold


        Comment

        Working...