[Tutor] Converting from unicode to nonstring

David Hutto smokefloat at gmail.com
Fri Oct 15 13:52:49 CEST 2010

On Thu, Oct 14, 2010 at 3:02 PM, Sander Sweers <sander.sweers at gmail.com> wrote:
> 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

After reading http://bugs.python.org/issue7935, I'll probably use
literal_eval. Thanks for pointing literal out.

More information about the Tutor mailing list