Jim <[EMAIL PROTECTED]> 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.

>>> csv.reader(StringIO.StringIO('a b c "d e"'), delimiter=' ').next()
['a', 'b', 'c', 'd e']

It can't quite do the second one, but:
>>> csv.reader(StringIO.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 -- [EMAIL PROTECTED] -- 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
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to