Apr 13, 1999, 6:01 AM
Post #4 of 7
(1913 views)
Permalink
I will now take this opportunity to revise my original post.
I wish to convert a string to a tuple.
My sample string should have been:
s = '(1, "abc\\tef", 2)'
instead of:
s = '(1, "abc\\def", 2)'
The problem is that the \\t part of the string gets expanded to a \011
Only the eval(string.replace(s, '\\', '\\\\')) seems to do the job. Any
comments?
sample code:
>>> s = '(1, "abc\\tef", 2)'
>>> eval(s)
(1, 'abc\011ef', 2)
s = '(1, "abc\\tef", 2)'
>>> eval(string.replace(s, '\\', '\\\\'))
(1, 'abc\\tef', 2)
>>> s = '(1, "abc\\tef", 2)'
>>> eval(s, {"__builtins__": {}})
(1, 'abc\011ef', 2)
>>> s = '(1, "abc\\tef", 2)'
>>> r = rexec.RExec()
>>> s = '(1, "abc\\tef", 2)'
>>> r = rexec.RExec()
>>> r.r_eval(s)
(1, 'abc\011ef', 2)
Fredrik Lundh <fredrik@pythonware.com> wrote in article
<00f501be857f$ff636f40$f29b12c2@pythonware.com>...
> Charles G Waldman wrote:
> > If you're concerned about safety (the "eval" could be evaluating any
> > Python code, possibly a hazard if the string is coming from user
> > input) then you don't want to use eval at all.
>
> try:
>
> result = eval(string, {"__builtins__": {}})
>
> or:
>
> import rexec
> r = rexec.RExec()
> result = r.r_eval(string)
>
> </F>
>
>