Search code examples
pythonpyparsing

In PyParsing, how to setParseAction on a delimitedList to convert it to a Python list


I'm trying to extract a delimitedString as a list using PyParsing as follows:

from pyparsing import *

string = "arm + mips + x86"
pattern = delimitedList(Word(printables), delim="+")("architectures")
result = pattern.parseString(string)
print(dict(result))

The problem is that this prints

{'architectures': (['arm', 'mips', 'x86'], {})}

which is the string representation of a ParseResult. However, I would like the result to be a Python list:

{'architectures': ['arm', 'mips', 'x86']}

I've looked into doing this with setParseAction, but I wasn't able to figure out how to achieve this using the API of that method. I would actually like to apply list() to the entire ParseResult, but the setParseAction functions have to have the original string, locations, and tokens as input (cf. http://pyparsing.wikispaces.com/HowToUsePyparsing).

How can I post-process the result to make it a list?


Solution

  • Converting to an answer PaulMcG's comment, result.asDict() yields the desired result.