split string at commas respecting quotes when string not in csv format

R. David Murray rdmurray at bitdance.com
Thu Mar 26 22:11:23 CET 2009


Tim Chase <python.list at tim.thechases.com> wrote:
>    r = re.compile(r"""
>      (\w+)
>      \s*=\s*(
>      "(?:[^"]*)"
>      |
>      [^,]+
>      )
>      """, re.VERBOSE)
>    results = [
>      (m.group(1), m.group(2).strip('"'))
>      for m in r.finditer(s)
>      ]
> 
> Things like internal quoting ('b="123\"456", c="123""456"') would 
> require a slightly smarter parser.

Thank you thank you.  I owe you a dinner if we are ever in the
same town (are you at Pycon?).

I'm not going to worry about the internal quotes unless it shows up in
the real data.  I'm pretty sure it's now allowed by the spec.

--
R. David Murray             http://www.bitdance.com




More information about the Python-list mailing list