473,804 Members | 3,228 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 2201

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
2590
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
2263
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
2217
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
1684
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
1999
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
3181
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
1597
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
1562
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
1288
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
5293
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
9588
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
10589
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
10340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10327
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10085
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
5527
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
5663
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4302
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3828
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.