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

  • Home
  • SEARCH
  • 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 8527799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:39:27+00:00 2026-06-11T08:39:27+00:00

I am trying to use pyparsing for the first time. My parser is not

  • 0

I am trying to use pyparsing for the first time.
My parser is not doing what I hope it would do, could someone please check and see what is wrong. I am trying to embedd OneOrMore within OneOrMore, which I think should work fine but it is not.

below is the whole code:

import pyparsing

status = """
    sale number       : 11/7 
    NAME               ID    PAWN    PRICE    TIME         %C     STATE     START/STOP
    cross-cu-1       1055       1    106284K  07:49:36.19  25.05%   run          1d01h
    cross-cu-2        918       1    104708K  07:38:19.08  24.02%   run          1d01h
    sale number       : 11/8 
    NAME               ID    PAWN    PRICE    TIME         %C     STATE     START/STOP
    cross-cu-3       1055       1    106284K  07:49:36.19  25.05%   run          1d01h
    cross-cu-4        918       1    104708K  07:38:19.08  24.02%   run          1d01h
    """

integer = pyparsing.Word(pyparsing.nums).setParseAction(lambda toks: int(toks[0]))
decimal = pyparsing.Word(pyparsing.nums + ".").setParseAction(lambda toks: float(toks[0]))
wordSuppress = pyparsing.Suppress(pyparsing.Word(pyparsing.alphas))
endOfLine = pyparsing.LineEnd().suppress()
colon = pyparsing.Suppress(":")

saleNumber = pyparsing.Regex("\d{2}\/\d{1}").setResultsName("saleNumber")
lineSuppress = pyparsing.Regex("NAME.*STOP") + endOfLine
saleRow = wordSuppress + wordSuppress + colon + saleNumber + endOfLine

name = pyparsing.Regex("cross-cu-\d").setResultsName("name")
id = integer.setResultsName("id")
pawn = integer.setResultsName("pawn")
price = integer.setResultsName("price") + "K"
time = pyparsing.Regex("\d{2}:\d{2}:\d{2}.\d{2}").setResultsName("time")
c = decimal.setResultsName("c") + "%"
state = pyparsing.Word(pyparsing.alphas).setResultsName("state")
startStop = pyparsing.Word(pyparsing.alphanums).setResultsName("startStop")
row = name + id + pawn + price + time + c + state + startStop + endOfLine

table = pyparsing.OneOrMore(pyparsing.Group(saleRow + lineSuppress.suppress() + (pyparsing.OneOrMore(pyparsing.Group(row) | pyparsing.SkipTo(row).suppress())) ) | pyparsing.SkipTo(saleRow).suppress())

resultDic = [x.asDict() for x in table.parseString(status)]
print resultDic

It returns only [{'saleNumber': '11/7'}]
I was hoping to get a list of dic like this:

[{ {'saleNumber': '11/7'},{ elements in cross-cu-1 line, elements in cross-cu-2 line } },
 { {'saleNumber': '11/8'},{ elements in cross-cu-3 line, elements in cross-cu-4 line } }]

Any help is appreciated!
Please don´t suggest other ways of implementing this output! I am trying to learn pyparsing as well!

  • 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-11T08:39:29+00:00Added an answer on June 11, 2026 at 8:39 am

    In this case pyparsing is probably overkill. Why don’t you simply read the file line by line and then parse the results?

    The code would look like this:

    EDIT: I have updated the code to follow your example more closely.

    from collections import defaultdict

    status = """
    sale number       : 11/7
    NAME               ID    PAWN    PRICE    TIME         %C     STATE     START/STOP
    cross-cu-1       1055       1    106284K  07:49:36.19  25.05%   run          1d01h
    cross-cu-2        918       1    104708K  07:38:19.08  24.02%   run          1d01h
    sale number       : 11/8
    NAME               ID    PAWN    PRICE    TIME         %C     STATE     START/STOP
    cross-cu-3       1055       1    106284K  07:49:36.19  25.05%   run          1d01h
    cross-cu-4        918       1    104708K  07:38:19.08  24.02%   run          1d01h
    """
    
    sale_number = ''
    
    sales = defaultdict(list)
    
    for line in status.split('\n'):
        line = line.strip()
        if line.startswith("NAME"):
             continue
        elif line.startswith("sale number"):
             sale_number = line.split(':')[1].strip()
        elif not line or line.isspace() :
             continue
        else:
             # you can also use a regular expression here
             sales[sale_number].append(line.split())
    
    for sale in sales:
        print sale, sales[sale]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use the Spirit library for the first time. I am
I'm trying to to use this class http://robbyonrails.com/articles/2005/05/11/parsing-a-rss-feed but am not sure where to
This is the first time I've ever tried to use JAXB for anything at
I am just trying to read .csv file first time.I have gone through the
I am trying to learn pyparsing. It sounds promising and something that would be
I'm trying to use pyparsing to parse function calls in the form: f(x, y)
I am trying to use FileHelper library for parsing a text file. Ultimately the
I'm working on a project that is trying to use context-free grammars for parsing
i'm trying use facebook API to upload photo in my fan page. I downloaded
I am trying use gem tire to search in my application. I have tables

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.