pyparsing wrong output

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Fri Feb 12 19:41:16 EST 2010


En Fri, 12 Feb 2010 10:41:40 -0300, Eknath Venkataramani  
<eknath.iyer at gmail.com> escribió:

> I am trying to write a parser in pyparsing.
> Help Me. http://paste.pocoo.org/show/177078/ is the code and this is  
> input
> file: http://paste.pocoo.org/show/177076/ .
> I get output as:
> <generator object at 0xb723b80c>

There is nothing wrong with pyparsing here. scanString() returns a  
generator, like this:

py> g = (x for x in range(20) if x % 3 == 1)
py> g
<generator object <genexpr> at 0x00E50D78>

A generator is like a function that may be suspended and restarted. It  
yields one value at a time, and only runs when you ask for the next value:

py> next(g)
1
py> next(g)
4

You may use a `for` loop to consume the generator:

py> for i in g:
...   print i
...
7
10
13
16
19

Once it run to exhaustion, asking for more elements always raises  
StopIteration:

py> next(g)
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
StopIteration

Try something like this:

results = x.scanString(filedata)
for result in results:
     print result

See http://docs.python.org/tutorial/classes.html#generators

-- 
Gabriel Genellina




More information about the Python-list mailing list