Python/PyParsing: Difficulty with setResultsName
Posted
by Rosarch
on Stack Overflow
See other posts from Stack Overflow
or by Rosarch
Published on 2010-05-30T20:24:01Z
Indexed on
2010/05/30
20:32 UTC
Read the original article
Hit count: 309
I think I'm making a mistake in how I call setResultsName()
:
from pyparsing import *
DEPT_CODE = Regex(r'[A-Z]{2,}').setResultsName("Dept Code")
COURSE_NUMBER = Regex(r'[0-9]{4}').setResultsName("Course Number")
COURSE_NUMBER.setParseAction(lambda s, l, toks : int(toks[0]))
course = DEPT_CODE + COURSE_NUMBER
course.setResultsName("course")
statement = course
From IDLE:
>>> myparser import *
>>> statement.parseString("CS 2110")
(['CS', 2110], {'Dept Code': [('CS', 0)], 'Course Number': [(2110, 1)]})
The output I hope for:
>>> myparser import *
>>> statement.parseString("CS 2110")
(['CS', 2110], {'Course': ['CS', 2110], 'Dept Code': [('CS', 0)], 'Course Number': [(2110, 1)]})
Does setResultsName()
only work for terminals?
© Stack Overflow or respective owner