gry@ll.mit.edu wrote:
> I have a string like:
> {'the','dog\'s','bite'}
> or maybe:
> {'the'}
> or sometimes:
> {}
...
> I want to end up with a python array of strings like:
>
> ['the', "dog's", 'bite']
Assuming that you trust the input, you could always use eval,
but since it seems fairly ea
George -
Thanks for your enthusiastic endorsement!
Here are some quibbles about your pyparsing grammar (but really, not
bad for a first timer):
1. The Word class is used to define "words" or collective groups of
characters, by specifying what sets of characters are valid as leading
and/or body ch
PyParsing rocks! Here's what I ended up with:
def unpack_sql_array(s):
import pyparsing as pp
withquotes = pp.dblQuotedString.setParseAction(pp.removeQuotes)
withoutquotes = pp.CharsNotIn('",')
parser = pp.StringStart() + \
pp.Word('{').suppress() + \
pp.
Ah, this is much better than my crude replace technique. I forgot
about str.decode().
Thanks!
-- Paul
--
http://mail.python.org/mailman/listinfo/python-list
Paul McGuire wrote:
text = r"'the','dog\'s','bite'"
def unquote(s,l,t):
>
> ... t2 = t[0][1:-1]
> ... return t2.replace("\\'","'")
> ...
Note also, that the codec 'string-escape' can be used to do what's done
with str.replace in this example:
py> s
"'the','dog\\'s','bite'"
py> s
Pyparsing includes some built-in quoted string support that might
simplify this problem. Of course, if you prefer regexp's, I'm by no
means offended!
Check out my Python console session below. (You may need to expand the
unquote method to do more handling of backslash escapes.)
-- Paul
(Downloa
gry@ll.mit.edu writes:
> I have a string like:
> {'the','dog\'s','bite'}
> or maybe:
> {'the'}
> or sometimes:
> {}
>
> [FYI: this is postgresql database "array" field output format]
>
> which I'm trying to parse with the re module.
> A single quoted string would, I think, be:
> r"\{'([^']|\\'
gry@ll.mit.edu wrote:
> I have a string like:
> {'the','dog\'s','bite'}
> or maybe:
> {'the'}
> or sometimes:
> {}
>
[snip]
>
> I want to end up with a python array of strings like:
>
> ['the', "dog's", 'bite']
>
> Any simple clear way of parsing this in python would be
> great; I just assume
I have a string like:
{'the','dog\'s','bite'}
or maybe:
{'the'}
or sometimes:
{}
[FYI: this is postgresql database "array" field output format]
which I'm trying to parse with the re module.
A single quoted string would, I think, be:
r"\{'([^']|\\')*'\}"
but how do I represent a *sequence* of