python - Turning a string into list of positive and negative numbers -
if have string in form 'a,b,c,d,e'
letters positive or negative numbers, e.g '1,-2,3,4,-5'
, want turn tuple e.g (1,-2,3,4,-5), how this?
one method use ast.literal_eval
:
safely evaluate expression node or string containing python expression. string or node provided may consist of following python literal structures: strings, numbers, tuples, lists, dicts, booleans, ,
none
.this can used safely evaluating strings containing python expressions untrusted sources without need parse values oneself.
>>> import ast >>> ast.literal_eval('1,-2,3,4,-5') (1, -2, 3, 4, -5)
Comments
Post a Comment