Sion Arrowsmith wrote:
> >>> 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.
IMO, this solution is on the right track.
FWIW, the StringIO wrapper is unnecessary.
Any iterable wi
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 c
Jim wrote:
> Is there some easy way to split a line, keeping together double-quoted
> strings?
Thank you for the replies.
Jim
--
http://mail.python.org/mailman/listinfo/python-list
> > 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,
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.
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]
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.iss
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
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
--
http: