Is there a simple way to parse this string ?
josepharmbruster at gmail.com
josepharmbruster at gmail.com
Sat Dec 22 10:21:26 EST 2007
Steven D'Aprano,
On Dec 21, 2:08 am, Steven D'Aprano
<ste... at REMOVE.THIS.cybersource.com.au> wrote:
> On Thu, 20 Dec 2007 20:27:23 -0800, josepharmbrus... at gmail.com wrote:
> > Stef,
>
> > For clarification, there is nothing hazardous about using eval on the
> > string that you presented.
>
> > t = eval('(0, 0, 0, 255), (192, 192, 192, 255), True, 8')
>
> > Whether or not this is the "simplest" solution, remains a question.
>
> For clarification, if all the poster wanted was to convert the *specific*
> *known* string to a tuple, he would be better off just writing it as a
> tuple:
Steven,
No, that's not what he asked. Read the original question.
>
> t = (0, 0, 0, 255), (192, 192, 192, 255), True, 8
>
> is much faster than calling eval().
>
> But obviously that's not what the Original Poster wants to do.
There's nothing "Obviously" Implied about what the author wants to do
here, besides convert an innocent string object to a tuple.
> The tuple
> give was indicative of input that comes from somewhere
Really? I personally can't tell that from his provided example.
There's definitely not enough info on this one.
> -- perhaps a
> config file, perhaps a web form, perhaps a command line argument, who
> knows? The point is, if the string comes from a user, then it could
> contain anything:
>
> '(0, 0, 0, 255), (192, 192, 192, 255), True, 8'
> '1000, 10001, 100002, 1000004'
> '"foo bar baz".split()'
> '[i for i in range(100000)]'
> '[19852.7412]*100000**2'
> '__import__("os").system("ls -r *")'
>
> Just because the OP's specific example is safe doesn't make eval() safe.
Agreed. And after the last couple comments, he was probably made
aware of that. Thank you for reiterating :-)
>
> --
> Steven
More information about the Python-list
mailing list