newbie question

Tim Chase python.list at tim.thechases.com
Thu Mar 24 06:58:36 EDT 2016


On 2016-03-24 11:49, David Palao wrote:
>> s = "(1, 2, 3, 4)"
>>
>> and I want to recover the tuple in a variable t
>>
>> t = (1, 2, 3, 4)
>>
>> how would you do ?
>
> Use "eval":
> s = "(1, 2, 3, 4)"
> t = eval(s)

Using eval() has security implications. Use ast.literal_eval for
safety instead:

  import ast
  s = "(1, 2, 3, 4)"
  t = ast.literal_eval(s)

-tkc





More information about the Python-list mailing list