#49 commaSeparatedList.parseString error

closed-works-for-me
nobody
None
5
2012-06-16
2012-01-17
K Yoder
No

Hello,

Using pyparsing 1.5.6 and python 2.7.2 I am seeing incorrect parsing behavior:

>>> str = """'field1', 'field2, with comma and :\\'"""
>>> str
"'field1', 'field2, with comma and :\\'"
>>> p = pyparsing.commaSeparatedList.parseString(str)
>>> p
(["'field1'", "'field2", "with comma and :\\'"], {})

This ought to have been two parsed fields, not three. The trailing backslash followed by quote mark appears to be the cause of the problem.

Discussion

  • Paul McGuire
    Paul McGuire
    2012-06-16

    This is not a bug, your backslash is escaping the closing single-quote, so pyparsing does not recognize the quoted text.

    >>> str = """'field1', 'field2, with comma and :\\'"""
    >>> print str
    'field1', 'field2, with comma and :\'

    To test with strings containing backslashes, it is easier to work with Python raw string literals:

    >>> str = r"""'field1', 'field2, with comma and :\\'"""
    >>> print str
    'field1', 'field2, with comma and :\\'
    >>> print commaSeparatedList.parseString(str).asList()
    ["'field1'", "'field2, with comma and :\\\\'"]

    With the backslash properly escaped (so that it does not escape the closing single-quote), field2 is parsed correctly.

     
  • Paul McGuire
    Paul McGuire
    2012-06-16

    • status: open --> closed-works-for-me