split a line, respecting double quotes

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

    #1

    split a line, respecting double quotes

    Is there some easy way to split a line, keeping together double-quoted
    strings?

    I'm thinking of
    'a b c "d e"' --['a','b','c','d e']
    .. I'd also like
    'a b c "d \" e"' --['a','b','c','d " e']
    which omits any s.split('"')-based construct that I could come up with.

    Thank you,
    JIm

  • faulkner

    #2
    Re: split a line, respecting double quotes

    import re
    re.findall('\". *\"|\S+', raw_input())

    Jim wrote:
    Is there some easy way to split a line, keeping together double-quoted
    strings?
    >
    I'm thinking of
    'a b c "d e"' --['a','b','c','d e']
    . I'd also like
    'a b c "d \" e"' --['a','b','c','d " e']
    which omits any s.split('"')-based construct that I could come up with.
    >
    Thank you,
    JIm

    Comment

    • vbgunz

      #3
      Re: split a line, respecting double quotes

      Jim wrote:
      Is there some easy way to split a line, keeping together double-quoted
      strings?
      using the re module I find this to probably be the easiest but in no
      way is this gospel :)

      import re
      rex = re.compile(r'(" .*?"|\S)')
      sub = 'a b c "d e"'
      res = [x for x in re.split(rex, sub) if not x.isspace()][1:-1]
      print res # -['a', 'b', 'c', '"d e"']

      basically import the re module, compile a pattern, identify a string,
      create a list comprehension with a filter, slice out the result and
      print to screen. I hope this helps.

      Comment

      • faulkner

        #4
        Re: split a line, respecting double quotes

        sorry, i didn't read all your post.
        def test(s):
        res = ['']
        in_dbl = False
        escaped = False
        for c in s:
        if in_dbl:
        if escaped:
        res[-1] += c
        if c != '\\':
        escaped = False
        else:
        res[-1] += c
        if c == '\\':
        escaped = True
        elif c == '"':
        res.append('')
        in_dbl = False
        elif c == ' ':
        res.append('')
        elif c == '"':
        res.append('')
        res[-1] += c
        in_dbl = True
        else:
        res[-1] += c
        while '' in res:
        res.remove('')
        return res

        faulkner wrote:
        import re
        re.findall('\". *\"|\S+', raw_input())
        >
        Jim wrote:
        Is there some easy way to split a line, keeping together double-quoted
        strings?

        I'm thinking of
        'a b c "d e"' --['a','b','c','d e']
        . I'd also like
        'a b c "d \" e"' --['a','b','c','d " e']
        which omits any s.split('"')-based construct that I could come up with.

        Thank you,
        JIm

        Comment

        • Steven Bethard

          #5
          Re: split a line, respecting double quotes

          Jim wrote:
          Is there some easy way to split a line, keeping together double-quoted
          strings?
          >
          I'm thinking of
          'a b c "d e"' --['a','b','c','d e']
          . I'd also like
          'a b c "d \" e"' --['a','b','c','d " e']
          which omits any s.split('"')-based construct that I could come up with.
          >>import shlex
          >>shlex.split(' a b c "d e"')
          ['a', 'b', 'c', 'd e']
          >>shlex.split(r 'a b c "d \" e"')
          ['a', 'b', 'c', 'd " e']

          Note that I had to use a raw string in the latter case because otherwise
          there's no real backslash in the string::
          >>'a b c "d \" e"'
          'a b c "d " e"'
          >>r'a b c "d \" e"'
          'a b c "d \\" e"'

          STeVe

          Comment

          • vbgunz

            #6
            Re: split a line, respecting double quotes

            Is there some easy way to split a line, keeping together double-quoted
            strings?
            import re
            rex = re.compile(r'(" .*?"|\S)')
            sub = 'a b c "d e"'
            res = [x for x in re.split(rex, sub) if not x.isspace()][1:-1]
            print res # -['a', 'b', 'c', '"d e"']
            instead of slicing the result out, you use this too:
            res = [x for x in re.split(rex, sub) if x[0:].strip()]

            Comment

            • Jim

              #7
              Re: split a line, respecting double quotes


              Jim wrote:
              Is there some easy way to split a line, keeping together double-quoted
              strings?
              Thank you for the replies.

              Jim

              Comment

              • Sion Arrowsmith

                #8
                Re: split a line, respecting double quotes

                Jim <jhefferon@smcv t.eduwrote:
                >Is there some easy way to split a line, keeping together double-quoted
                >strings?
                >
                >I'm thinking of
                'a b c "d e"' --['a','b','c','d e']
                >. I'd also like
                'a b c "d \" e"' --['a','b','c','d " e']
                >which omits any s.split('"')-based construct that I could come up with.
                >>csv.reader(St ringIO.StringIO ('a b c "d e"'), delimiter=' ').next()
                ['a', 'b', 'c', 'd e']

                It can't quite do the second one, but:
                >>csv.reader(St ringIO.StringIO ('a b c "d "" e"'), delimiter=' ').next()
                ['a', 'b', 'c', 'd " e']
                isn't far off.

                On the other hand, it's kind of a stupid solution. I'd really go with
                shlex as someone suggested up thread.

                --
                \S -- siona@chiark.gr eenend.org.uk -- http://www.chaos.org.uk/~sion/
                ___ | "Frankly I have no feelings towards penguins one way or the other"
                \X/ | -- Arthur C. Clarke
                her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump

                Comment

                • Raymond Hettinger

                  #9
                  Re: split a line, respecting double quotes

                  Sion Arrowsmith wrote:
                  >csv.reader(Str ingIO.StringIO( 'a b c "d "" e"'), delimiter=' ').next()
                  ['a', 'b', 'c', 'd " e']
                  isn't far off.
                  >
                  On the other hand, it's kind of a stupid solution.
                  IMO, this solution is on the right track.
                  FWIW, the StringIO wrapper is unnecessary.
                  Any iterable will do:
                  reader(['a b c "d e"'], delimiter=' ')


                  Raymond

                  Comment

                  Working...