wasm-demo/demo/ermis-f/python_m/cur/1129

69 lines
1.6 KiB
Plaintext

From: bhuzyk at kodak.com (Bruce Huzyk)
Date: 13 Apr 1999 13:01:39 GMT
Subject: Converting a string to a tuple
References: <01be8530$65bb7120$52037e81@saints> <14098.28727.245158.616727@buffalo.fnal.gov> <00f501be857f$ff636f40$f29b12c2@pythonware.com>
Message-ID: <01be85b5$f7197c90$52037e81@saints>
Content-Length: 1253
X-UID: 1129
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 at pythonware.com> wrote in article
<00f501be857f$ff636f40$f29b12c2 at 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>
>
>