Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Problem with "eval"

0 views
Skip to first unread message

Lemuel Elesterio

unread,
Jul 14, 1998, 3:00:00 AM7/14/98
to
>>> glist = '[["monitor", "a 17 inch monitor"],["desktop","Compaq"]]'
>>> eval(glist)
[['monitor', 'a 17 inch monitor'], ['desktop', 'Compaq']]

- no problemo. But...


>>> slist = '[["monitor", "a 17" monitor"],["desktop","HP Vectra"]]'
>>> eval(slist)
Traceback (innermost last):
File "<stdin>", line 1, in ?
File "<string>", line 1
[["monitor", "a 17" monitor"],["desktop","HP Vectra"]]
^
SyntaxError: invalid syntax

- the problem is caused by an extra double quote - a 17" monitor

Please suggest a way of solving this. Take note that a string of list
is always given to us and therefore we have no control in changing its
format.

Thank you and more power to Python!

vcard.vcf

Tim Peters

unread,
Jul 14, 1998, 3:00:00 AM7/14/98
to
[Lemuel Elesterio]

> ...
> >>> slist = '[["monitor", "a 17" monitor"],["desktop","HP Vectra"]]'
> >>> eval(slist)
> Traceback (innermost last):
> File "<stdin>", line 1, in ?
> File "<string>", line 1
> [["monitor", "a 17" monitor"],["desktop","HP Vectra"]]
> ^
> SyntaxError: invalid syntax
>
> - the problem is caused by an extra double quote - a 17" monitor
>
> Please suggest a way of solving this. Take note that a string of list
> is always given to us and therefore we have no control in changing its
> format.

Then you're going to have to parse the string yourself and *guess* which
quotes are "real" -- Python 1.5.1 is many things, but Guido isn't adding
telepathy until 1.7 <wink>.

In general, ambiguous parsing is a very hard problem, and you're either
going to have to live with errors, or find a way to get non-insane input.
No matter how hard you think the latter is, in the end it will be easier
than the former <0.5 wink>.

The attached function will, e.g., change the string

[["monitor", ""a 17"-21" monitor""],["desktop","HP Vectra"]]

into the string

[["monitor", "\"a 17\"-21\" monitor\""],["desktop","HP Vectra"]]

which is probably what was intended. It will screw up badly, though, if an
internal quote is eventually followed by a comma or right bracket before the
intended string ends. You can try to fix this, but it's likely that each
attempt will just lead to more complex code that makes subtler mistakes. In
the end, it may work better to try all possible ways of escaping quotes,
throw out the ways Python refuses to compile (use the "compile" built-in for
this, in a try block), then guess which of the survivors is most reasonable.
E.g., if you *know* that each inner list is supposed to contain exactly two
strings, the problem is much easier, although still inherently ambiguous.
For example, then the inner list

["big", "fat", "cat"]

may have been intended to be either of

["big", "fat\", \"cat"]
["big\", \"fat", "cat"]

and there's no principled way to pick one.

Have fun <wink>.

garbage-in-garbage-out-ly y'rs - tim

import re
_quotesucker = re.compile(r"""
" # get an open quote
(?P<guts> .*? ) # and as few chars as possible until a close quote,
" # which is a quote followed by ...
\s* # some amount of whitespace followed by ...
[],] # a right bracket or comma
""", re.VERBOSE).search

def quoteguesser(s):
from string import replace, find
pos = 0
while 1:
# guess the next string
m = _quotesucker(s, pos)
if not m:
break
# escape all the quotes in the guts
start, end = m.span('guts')
guts = s[start:end]
if find(guts, '"') >= 0:
guts = replace(guts, '"', '\\"')
s = s[:start] + guts + s[end:]
pos = start + len(guts) + 1 # 1 beyond end quote
return s

Cyril Slobin

unread,
Jul 14, 1998, 3:00:00 AM7/14/98
to
"Tim Peters" <tim...@email.msn.com> wrote:

> Then you're going to have to parse the string yourself and *guess* which
> quotes are "real" -- Python 1.5.1 is many things, but Guido isn't adding
> telepathy until 1.7 <wink>.

Ergh! Stop it NOW! Perl already had built-in guesser long ago! ;-)
(I mean that nice /$foo[bar]/ syntax ambiguity.)

Envying-Tim's-taglines-ly - Cyril

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp Create Your Own Free Member Forum

0 new messages