split a line, respecting double quotes

vbgunz vbgunz at gmail.com
Fri Jul 7 18:00:54 EDT 2006


> > 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()]




More information about the Python-list mailing list