Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8270131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:28:36+00:00 2026-06-08T06:28:36+00:00

I learned that in pyparsing, you can name an element/group/node by doing this: token

  • 0

I learned that in pyparsing, you can name an element/group/node by doing this:

token = pyparsing.Literal("Foobar")("element_name_here")

So, I made a sample program to test it out:

import pyparsing as pp

Prefix = pp.Word(pp.nums)("Prefix")
Name = pp.Literal("FOOBAR")("Name")
Modifier = pp.Word(pp.alphas)("Modifier")
Modifier_Group = pp.Group(pp.OneOrMore(Modifier))("Modifier_Group")
Sentence = pp.Group(pp.Optional(Prefix) + Name + Modifier_Group)("Sentence")

out = Sentence.parseString("123 FOOBAR testA testB")

Then, I tried getting the output with these named tokens.

I tried this:

>>> print out
[['123', 'FOOBAR', ['testA', 'testB']]]

…but that doesn’t get me the token names.

I then tried doing the following:

>>> print out.items()
[('Sentence', (['123', 'FOOBAR', (['testA', 'testB'], {'Modifier': [('testA', 0), 
('testB', 1)]})], {'Modifier_Group': [((['testA', 'testB'], {'Modifier': [('testA', 0),
('testB', 1)]}), 2)], 'Prefix': [('123', 0)], 'Name': [('FOOBAR', 1)]}))]

>>> print dict(out)

{'Sentence': (['123', 'FOOBAR', (['testA', 'testB'], {'Modifier': [('testA', 0), 
('testB', 1)]})], {'Modifier_Group': [((['testA', 'testB'], {'Modifier': [('testA', 0),
('testB', 1)]}), 2)], 'Prefix': [('123', 0)], 'Name': [('FOOBAR', 1)]})}

>>> import collections
>>> print collections.OrderedDict(out)
OrderedDict([('Sentence', (['123', 'FOOBAR', (['testA', 'testB'], {'Modifier': [
('testA', 0), ('testB', 1)]})], {'Modifier_Group': [((['testA', 'testB'], 
{'Modifier': [('testA', 0), ('testB', 1)]}), 2)], 'Prefix': [('123', 0)], 
'Name': [('FOOBAR', 1)]}))])

…but they contained a peculiar mixture of dicts, lists, and tuples, and I couldn’t figure out how to parse them. Then, I tried doing this:

>>> print out.asXML()
<Sentence>
  <Sentence>
    <Prefix>123</Prefix>
    <Name>FOOBAR</Name>
    <Modifier_Group>
      <Modifier>testA</Modifier>
      <Modifier>testB</Modifier>
    </Modifier_Group>
  </Sentence>
</Sentence>

…and that got me EXACTLY what I wanted, except that it’s in XML, instead of a python data structure that I can easily manipulate. Is there some way to get such a data structure (without having to parse the XML)?

I did find a solution that returns a nested dict, but dicts in python are unordered, (and I want the tokens in order), so it isn’t a solution for me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T06:28:37+00:00Added an answer on June 8, 2026 at 6:28 am

    Pyparsing returns a ParseResults object that already gives you that structure. You can visualize your sentence structure by printing out.dump():

    >>> print out.dump()
    [['123', 'FOOBAR', ['testA', 'testB']]]
    - Sentence: ['123', 'FOOBAR', ['testA', 'testB']]
      - Modifier_Group: ['testA', 'testB']
        - Modifier: testB
      - Name: FOOBAR
      - Prefix: 123
    

    You can access these elements as if they were keys in a dict:

    >>> print out.Sentence.keys()
    ['Modifier_Group', 'Prefix', 'Name']
    >>> print out.Sentence['Prefix']
    123
    

    or as attributes on an object:

    >>> print out.Sentence.Name
    FOOBAR
    >>> print out.Sentence.Prefix
    123
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I learned that the name attribute has been deprecated. But this is how I
I learned that Xcode can compile c code, that means can we write whole
I've learned that MySQL can compress communication between servers and clients. Compression is used
When I learned CGI I learned that any programing language can be used to
I've learned that Xor operation can be used to implement effective swap function. like
I just learned that OCAML have to have a . postfix for doing float
I learned that to configure c3p0 pooling in hibernate, we can have write the
I've learned that I can locate files bigger than X megabytes using: $ find
I just learned that an object can have only one ID, so I'm not
I recently learned that when using onclick, for a button, the field name and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.