split a line, respecting double quotes
faulkner
faulkner612 at comcast.net
Fri Jul 7 17:58:09 EDT 2006
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
More information about the Python-list
mailing list