Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Cannot access nested named elements #552

@Honza0297

Description

@Honza0297

When I define a named element inside another named element (e.g. pronoun inside sentence in the example below), I suppose I would be able to access the named elements recursively as shown in the example (parsed.sentence.verb).
Documentation does not describe this kind of situation, thus I am not sure whether this is a bug or a feature?
Thanks in advance for the answer!

Example below

from pyparsing import Word, alphas

# Define tokens
verb = Word(alphas).setResultsName("verb")
pronoun = Word(alphas).setResultsName("pronoun")
sentence = (pronoun + verb).setResultsName("sentence")

# Input string
input_str = "You are"

# Parse input
parsed = sentence.parseString(input_str)
# parsed.pprint()

# Access individual components
print(parsed.sentence.pronoun)  # Expected result: You, actual result: ' '
print(parsed.sentence.verb)     # Expected result: are, actual result: ' '

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions