[python 2.3.3, pyparsing 1.3]
I have:
def unpack_sql_array(s):
# unpack a postgres "array", e.g. "{'w1','w2','w3'}" into a
list(str)
import pyparsing as pp
withquotes = pp.dblQuotedString.setParseAction(pp.removeQuotes)
withoutquotes = pp.CharsNotIn('",{}')
parser = pp.StringStart() + \
pp.Literal('{').suppress() + \
pp.delimitedList(withquotes | withoutquotes) + \
pp.Literal('}').suppress() + \
pp.StringEnd()
return parser.parseString(s).asList()
which works beautifully, except on the input: "{}". How can I neatly
modify the parser to return an empty list in this case?
Yes, obviously, I could say
if s=='{}': return []
It just seems like I'm missing some simple intrinsic way to get this
out of the parser. I am hoping to become more skillful in using the
wonderful pyparsing module!
-- George Young 1 1487
To indicate that a particular parse expression may be empty, pyparsing
includes the Optional element.
Change:
pp.delimitedList(withquotes | withoutquotes) + \
to:
pp.Optional( pp.delimitedList(withquotes | withoutquotes) ) + \
and I think this will have the desired effect.
-- Paul
<gr*@ll.mit.edu> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com... [python 2.3.3, pyparsing 1.3] I have:
def unpack_sql_array(s): # unpack a postgres "array", e.g. "{'w1','w2','w3'}" into a list(str) import pyparsing as pp withquotes = pp.dblQuotedString.setParseAction(pp.removeQuotes) withoutquotes = pp.CharsNotIn('",{}') parser = pp.StringStart() + \ pp.Literal('{').suppress() + \ pp.delimitedList(withquotes | withoutquotes) + \ pp.Literal('}').suppress() + \ pp.StringEnd() return parser.parseString(s).asList()
which works beautifully, except on the input: "{}". How can I neatly modify the parser to return an empty list in this case? Yes, obviously, I could say if s=='{}': return [] It just seems like I'm missing some simple intrinsic way to get this out of the parser. I am hoping to become more skillful in using the wonderful pyparsing module!
-- George Young This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Lukas Holcik |
last post by:
Hi everyone!
How can I simply search text for regexps (lets say <a
href="(.*?)">(.*?)</a>) and save all URLs(1) and link contents(2) in a
dictionary { name : URL}? In a single pass if it could....
|
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...
|
by: Paul McGuire |
last post by:
"The best laid plans o' mice an' men / Gang aft a-gley"
So said Robert Burns (who really should do something about that speech
impediment!). And so said I about 6 weeks ago, when I thought that...
|
by: Khoa Nguyen |
last post by:
I am trying to come up with a grammar that describes the following:
record = f1,f2,...,fn END_RECORD
All the f(i) has to be in that order.
Any f(i) can be absent (e.g. f1,,f3,f4,,f6 END_RECORD)...
|
by: 7stud |
last post by:
To the developer:
1) I went to the pyparsing wiki to download the pyparsing module and
try it
2) At the wiki, there was no index entry in the table of contents for
Downloads. After searching...
| |
by: napolpie |
last post by:
DISCUSSION IN
USER nappie writes:
Hello, I'm Peter and I'm new in python codying and I'm using parsying
to extract data from one meteo Arpege file. This file is long file and
it's composed by...
|
by: Neal Becker |
last post by:
I'm just trying out pyparsing. I get stack overflow on my first try. Any
help?
#/usr/bin/python
from pyparsing import Word, alphas, QuotedString, OneOrMore, delimitedList
first_line = ''...
|
by: Just Another Victim of the Ambient Morality |
last post by:
Is pyparsing really a recursive descent parser? I ask this because
there are grammars it can't parse that my recursive descent parser would
parse, should I have written one. For instance:
...
|
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)...
|
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,...
|
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...
| |
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...
|
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...
|
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,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
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...
|
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...
| |
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 ...
| |