Dietmar Winkler
Dietmar Winkler

Reputation: 927

Getting a pure list out of 'pyparsing.ParseResults'

I'm currently trying to get a result from pyparsing as a pure list so I can flatten it. I read in the documentation that

ParseResults can also be converted to an ordinary list of strings by calling asList(). Note that this will strip the results of any field names that have been defined for any embedded parse elements. (The pprint module is especially good at printing out the nested contents given by asList().)

So I tried defining a setParseAction where I work on the ParseResult

what I get is:

>>> print type(tokens.args[0])
 <class 'pyparsing.ParseResults'>
>>> print type(tokens.args[0].asList)
 <type 'instancemethod'>

But I was expecting/needing the last one to be of type list. I must be missing something important when using asList() here.

Dietmar

PS: Here a MTC of what the tokens actually look like:

>>> print tokens.args[0]
['foo1', ['xxx'], ',', 'graphics={', 'bar1', ['xxx,yyy'], ',', 'bar2', 
['xxx,yyy'], ',', 'bar3', ['xxx,yyy,', 'zzz=baz', ['xxx,yyy']], '}']

Upvotes: 8

Views: 4106

Answers (2)

Niek de Klein
Niek de Klein

Reputation: 8824

Can you try

 print type(tokens.args[0].asList())

Upvotes: 5

Marcin
Marcin

Reputation: 49826

tokens.args[0].asList is a function. tokens.args[0].asList() is a call to that function (with no arguments beyond the self argument). It seems that you would like to know the type of that latter expression.

Upvotes: 9

Related Questions