473,721 Members | 2,263 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pyparsing Combine without merging sub-expressions

Within a larger pyparsing grammar, I have something that looks like::

wsj/00/wsj_0003.mrg

When parsing this, I'd like to keep around both the full string, and the
AAA_NNNN substring of it, so I'd like something like::
>>foo.parseStri ng('wsj/00/wsj_0003.mrg')
(['wsj/00/wsj_0003.mrg', 'wsj_0003'], {})

How do I go about this? I was using something like::
>>digits = pp.Word(pp.nums )
alphas = pp.Word(pp.alph as)
wsj_name = pp.Combine(alph as + '_' + digits)
wsj_path = pp.Combine(alph as + '/' + digits + '/' + wsj_name +
... '.mrg')

But of course then all I get back is the full path::
>>wsj_path.pars eString('wsj/00/wsj_0003.mrg')
(['wsj/00/wsj_0003.mrg'], {})

I could leave off the final Combine and add a parse action::
>>wsj_path = alphas + '/' + digits + '/' + wsj_name + '.mrg'
def parse_wsj_path( string, index, tokens):
... wsj_name = tokens[4]
... return ''.join(tokens) , wsj_name
...
>>wsj_path.setP arseAction(pars e_wsj_path)
wsj_path.pars eString('wsj/00/wsj_0003.mrg')
([('wsj/00/wsj_0003.mrg', 'wsj_0003')], {})

But that then allows whitespace between the pieces of the path, which
there shouldn't be::
>>wsj_path.pars eString('wsj / 00 / wsj_0003.mrg')
([('wsj/00/wsj_0003.mrg', 'wsj_0003')], {})

How do I make sure no whitespace intervenes, and still have access to
the sub-expression?

Thanks,

STeVe
Jan 20 '07 #1
3 2196

Steven Bethard wrote:
Within a larger pyparsing grammar, I have something that looks like::

wsj/00/wsj_0003.mrg

When parsing this, I'd like to keep around both the full string, and the
AAA_NNNN substring of it, so I'd like something like::
>>foo.parseStri ng('wsj/00/wsj_0003.mrg')
(['wsj/00/wsj_0003.mrg', 'wsj_0003'], {})

How do I go about this? I was using something like::
>>digits = pp.Word(pp.nums )
>>alphas = pp.Word(pp.alph as)
>>wsj_name = pp.Combine(alph as + '_' + digits)
>>wsj_path = pp.Combine(alph as + '/' + digits + '/' + wsj_name +
... '.mrg')

But of course then all I get back is the full path::
>>wsj_path.pars eString('wsj/00/wsj_0003.mrg')
(['wsj/00/wsj_0003.mrg'], {})
The tokens are what the tokens are, so if you want to replicate a
sub-field, then you'll need a parse action to insert it into the
returned tokens. BUT, if all you want is to be able to easily *access*
that sub-field, then why not give it a results name? Like this:

wsj_name = pp.Combine(alph as + '_' + digits).setResu ltsName("name")

Leave everything else the same, but now you can access the name field
independently from the rest of the combined tokens.

result = wsj_path.parseS tring('wsj/00/wsj_0003.mrg')
print result.dump()
print result.name
print result.asList()

-- Paul

Jan 21 '07 #2
Dennis Lee Bieber wrote:
On Sat, 20 Jan 2007 13:49:52 -0700, Steven Bethard
<st************ @gmail.comdecla imed the following in comp.lang.pytho n:
>Within a larger pyparsing grammar, I have something that looks like::

wsj/00/wsj_0003.mrg

When parsing this, I'd like to keep around both the full string, and the
AAA_NNNN substring of it, so I'd like something like::
> >>foo.parseStri ng('wsj/00/wsj_0003.mrg')
(['wsj/00/wsj_0003.mrg', 'wsj_0003'], {})
If working file name/paths, why not use the functions in os.path?
Two reasons. First, as I mentioned, this is within a larger pyparsing
grammar so it's not as easy to switch back and forth between the two.
Second, I do want to do some data validation (e.g. the name of the file
needs to be in a particular format) so I either need to post-process the
os.path approach or just do it in pyparsing.

>But that then allows whitespace between the pieces of the path, which
there shouldn't be::
If you didn't have whitespace coming in, there shouldn't be any
going out. If you do, you likely have malformed data and probably should
detect it earlier...
Well that's the intention of using pyparsing here. With a proper
grammar, pyparsing can detect the malformed data for me and throw an error.

STeVe
Jan 22 '07 #3
Paul McGuire wrote:
Steven Bethard wrote:
>Within a larger pyparsing grammar, I have something that looks like::

wsj/00/wsj_0003.mrg

When parsing this, I'd like to keep around both the full string, and the
AAA_NNNN substring of it, so I'd like something like::
> >>foo.parseStri ng('wsj/00/wsj_0003.mrg')
(['wsj/00/wsj_0003.mrg', 'wsj_0003'], {})

How do I go about this? I was using something like::
> >>digits = pp.Word(pp.nums )
>>alphas = pp.Word(pp.alph as)
>>wsj_name = pp.Combine(alph as + '_' + digits)
>>wsj_path = pp.Combine(alph as + '/' + digits + '/' + wsj_name +
... '.mrg')
[snip]
BUT, if all you want is to be able to easily *access*
that sub-field, then why not give it a results name? Like this:

wsj_name = pp.Combine(alph as + '_' + digits).setResu ltsName("name")

Leave everything else the same, but now you can access the name field
independently from the rest of the combined tokens.
Works great. Thanks!

STeVe
Jan 22 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2583
by: Peter Fein | last post by:
I'm trying to use pyparsing write a screenscraper. I've got some arbitrary HTML text I define as opener & closer. In between is the HTML data I want to extract. However, the data may contain the same characters as used in the closer (but not the exact same text, obviously). I'd like to get the *minimal* amount of data between these. Here's an example (whitespace may differ): from pyparsing import *
3
2256
by: phil_nospam_schmidt | last post by:
I am scanning text that has identifiers with a constant prefix string followed by alphanumerics and underscores. I can't figure out, using pyparsing, how to match for this. The example expression below seems to be looking for whitespace between the 'atod' and the rest of the identifier. identifier_atod = 'atod' + pp.Word('_' + pp.alphanums) How can I get pyparsing to match 'atodkj45k' and 'atod_asdfaw', but not 'atgdkasdjfhlksj' and...
7
2214
by: Steven Bethard | last post by:
How do I make sure that my entire string was parsed when I call a pyparsing element's parseString method? Here's a dramatically simplified version of my problem: py> import pyparsing as pp py> match = pp.Word(pp.nums) py> def parse_num(s, loc, toks): .... n, = toks .... return int(n) + 10 ....
2
1679
by: astarocean | last post by:
using pyparsing to deal with nested tables , wanna keep table's structure and propertys . but program was chunked with the </td> tag of inner table. have any ideas? here's the program from pyparsing import *
3
1995
by: rh0dium | last post by:
Hi all, I have a file which I need to parse and I need to be able to break it down by sections. I know it's possible but I can't seem to figure this out. The sections are broken by <> with one or more keywords in the <>. What I want to do is to be able to pars a particular section of the file. So for example I need to be able to look at the SYSLIB section. Presumably the sections are
2
3175
by: Andi Clemens | last post by:
Hi, we had some problems in the last weeks with our mailserver. Some messages were not delivered and we wanted to know why. But looking through the logfile is a time consuming process. So I wanted to write a parser to analyse the logs and parse them as XML. But I have never written a parser before and know I'm sitting in front of the logfile trying to write the grammar for pyparsing.
4
1589
by: Bytter | last post by:
Hi, I'm trying to construct a parser, but I'm stuck with some basic stuff... For example, I want to match the following: letter = "A"..."Z" | "a"..."z" literal = letter+ include_bool := "+" | "-" term = literal
19
1551
by: Ant | last post by:
Hi all, I have a question on PyParsing. I am trying to create a parser for a hierarchical todo list format, but have hit a stumbling block. I have parsers for the header of the list (title and description), and the body (recursive descent on todo items). Individually they are working fine, combined they throw an exception. The code follows:
3
1283
by: name | last post by:
Hi, I try to parse a file with pyparsing and get this output: - alias: host alias xyz - host_name: - ip_address: - use:
4
5278
by: Marek Kubica | last post by:
Hi, I am trying to get this stuff working, but I still fail. I have a format which consists of three elements: \d{4}M?-\d (4 numbers, optional M, dash, another number) EMPTY (the <EMPTYtoken) (the <PAGEBREAKtoken. The line may contain whitespaces, but nothing else)
0
8738
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9376
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9085
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8026
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5994
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4499
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4762
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2596
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2146
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.