Re: help with pyparsing



On Oct 31, 6:59 am, Neal Becker <ndbeck...@xxxxxxxxx> wrote:
I'm just trying out pyparsing. I get stack overflow on my first try. Any
help?

#/usr/bin/python

from pyparsing import Word, alphas, QuotedString, OneOrMore, delimitedList

first_line = '[' + delimitedList (QuotedString) + ']'

def main():
string = '''[ 'a', 'b', 'cdef']'''
greeting = first_line.parseString (string)
print greeting

if __name__ == "__main__":
main()

/usr/lib/python2.5/site-packages/pyparsing.py:2727: SyntaxWarning: Cannot
add element of type <type 'type'> to ParserElement
return ( expr + ZeroOrMore( Suppress( delim ) + expr ) ).setName(dlName)
/usr/lib/python2.5/site-packages/pyparsing.py:1008: SyntaxWarning: Cannot
add element of type <type 'type'> to ParserElement
return other + self
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/tmp/python-k3AVeY.py", line 5, in <module>
first_line = '[' + delimitedList (QuotedString) + ']'
File "/usr/lib/python2.5/site-packages/pyparsing.py", line 2727, in
delimitedList
return ( expr + ZeroOrMore( Suppress( delim ) + expr ) ).setName(dlName)
File "/usr/lib/python2.5/site-packages/pyparsing.py", line 1008, in
__radd__
return other + self
File "/usr/lib/python2.5/site-packages/pyparsing.py", line 1008, in
__radd__
return other + self

Oh! You are so close!

QuotedString is a class. delimitedList does not accept a class, but
an expression, which is usually created using instances of the
pyparsing classes. quotedString is an expression, defining a pretty
complex quoted string syntax (single or double-quoted string).

Just change QuotedString to quotedString throughout your script, and
try again.

-- Paul

.



Relevant Pages

  • Re: How to split a string containing nested commas-separated substrings
    ... I'd like to split a string by commas, but only at the "top level" so ... An element can be a comma-less substring, ... If some element contains commas, I don't want to split it. ... Group, delimitedList, quotedString ...
    (comp.lang.python)
  • Re: How to convert string to list without eval or exec
    ... "Oliver Kurz" wrote in message ... > could someone give me a solution how to convert a string to a list without ... from pyparsing import quotedString, Forward, Literal,delimitedList,Group ... # add more things to listItem, such as integers, etc. if your list has other ...
    (comp.lang.python)
  • Re: Quote-aware string splitting
    ... Here is a pyparsing example with some added test ... Pyparsing's quotedString built-in handles single or double ... quotes (if you don't want to be this permissive, ... meaning of "life"', 'grail'] ...
    (comp.lang.python)
  • Re: parse lines to name value pairs
    ... .from pyparsing import quotedString, printables, Word, OneOrMore, ... .for testdata in (line1, line2): ...
    (comp.lang.python)
  • Re: best way to parse a function-call-like string?
    ... Pyparsing will easily carve up these function declarations, ... give you some room for easy extension once your parsing job starts to ... Optional, Literal, Suppress, quotedString ...
    (comp.lang.python)