[Tutor] Converting from unicode to nonstring

Sander Sweers sander.sweers at gmail.com
Thu Oct 14 21:02:50 CEST 2010


On 14 October 2010 20:29, David Hutto <smokefloat at gmail.com> wrote:
> Actually, I needed it to be converted to something without a string
> attached to it. See a post above, and it was fixed by eval(),

Using eval is a big security risk and is generally not recommended for
any production code. What do you think eval() return for your string?
A tuple of ints which for your use case serves the same purpose as
using the list comprehension.

If you really want (you really don't) to use eval() then at least use
the safe one from the ast mode.

>>> from ast import literal_eval
>>> literal_eval(u'1,2,3,4')
(1, 2, 3, 4)
>>> eval(u'1,2,3,4')
(1, 2, 3, 4)

As you can see for your use case both return a tuple of ints.

Greets
Sander


More information about the Tutor mailing list