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 8927781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:17:24+00:00 2026-06-15T08:17:24+00:00

I’m new to Pyparsing (and pretty new to Python). I have tried to reduce

  • 0

I’m new to Pyparsing (and pretty new to Python). I have tried to reduce my problem down to the simplest form that will illustrate what’s going wrong (to the point where I probably wouldn’t need Pyparsing at all!)

Suppose I’ve got a string consisting of letters and numbers, such as “b7 z4 a2 d e c3”. There’s always a letter, but the number is optional. I want to parse this into its individual elements, and then process them, but where there is a bare letter, with no number, it would be handy to change it so that it had the “default” number 1 after it. Then I could process every element in a consistent way. I thought I could do this with a setparseAction, as follows:

from pyparsing import *
teststring = "a2 b5 c9 d e z"
expected_letter = Word("ABCDEFGabcdefgzZxy", exact=1)
expected_number = Word(nums)
letter_and_number = expected_letter + expected_number
bare_letter = expected_letter
bare_letter.setParseAction( lambda s,l,t:  t.append("1") )
elements =  letter_and_number | bare_letter
line = OneOrMore(elements)
print line.parseString(teststring)

Unfortunately, the t.append() doesn’t do what I’m expecting, which was to add a “1” to the list of parsed tokens. Instead, I get an error: TypeError: ‘str’ object is not callable.

I’m probably just being really thick, here, but could one of you experts please set me straight.

Thanks

Steve

  • 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-15T08:17:26+00:00Added an answer on June 15, 2026 at 8:17 am

    One of the basic concepts to get about pyparsing is that it does not work with just lists of strings, but assembles the parsed pieces into a ParseResults object. ParseResults is a rich data type defined in pyparsing, that can be accessed as a list, or as a dict or object if there are tokens that have been parsed from a ParserElement with a defined results name.

    However, while ParseResults was designed with easy access in mind, it is limited in ways it can be updated. Internally in pyparsing, each expression that matches creates a small ParseResults object; if this is part of a large expression, that expression accumulates the pieces into a large ParseResults using the += operator.

    In your case, you can append to the ParseResults that is passed in by creating a small ParseResults containing “1” and adding it to t:

    t += ParseResults("1")
    

    Unfortunately, this won’t work as a lambda – you could try

    lambda s,l,t: t.__iadd__(ParseResults("1"))
    

    But this feels a little too clever.

    You might also rethink your parser a bit, to take advantage of the Optional class. Think of your trailing digit as an optional element, for which you can define a default value to provide in case the element is missing. I think you can define what you want with just:

    >>> letter = Word(alphas,exact=1)
    >>> digit = Word(nums,exact=1)
    >>> teststring= "a2 b5 c9 d e z"
    >>> letter_and_digit = Combine(letter + Optional(digit, default="1"))
    >>> print (sum(letter_and_digit.searchString(teststring)))
    ['a2', 'b5', 'c9', 'd1', 'e1', 'z1']
    

    Combine is used to rejoin the separate letters and digits into strings, otherwise each match would look like ['a','2'], ['b','5'], etc.

    (Normally, searchString returns a list of ParseResults objects, which would look like a list of single-element lists. By passing the results of searchString to sum this adds them all into just one ParseResults of strings.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I've tracked down a weird MySQL problem to the two different ways I was
I have a text area in my form which accepts all possible characters from
I have been unable to fix a problem with Java Unicode and encoding. The
I need a function that will clean a strings' special characters. I do NOT

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.