Python eval function usage

Python eval function purpose:

The eval function converts strings into lists, tuples and dictionaries

Examples are as follows:

Can convert list, tuple, dict and string to each other.
# ################################################ 
characters Convert string to list
 >>>a = " [[1,2], [3,4], [5,6], [7,8], [9,0]] " 
>>> type(a)
 < type ' str ' >
>>> b = eval(a)
>>> print b
[[ 1, 2], [3, 4], [5, 6], [7, 8], [9 , 0]]
 >>> type(b)
 <type ' list ' >
 # ##### ########################################### 
String to dictionary
 >> > a = " {1: 'a', 2: 'b'} " 
>>> type(a)
 <type ' str ' >
>>> b = eval(a)
>>> print b
{ 1: ' a ' , 2: ' b ' }
 >>> type(b)
 <type ' dict ' >
 # ####################### ######################### 
String into tuple
 >>> a = " ([1,2], [3,4], [5,6], [7,8], (9,0)) " 
>>> type(a)
 <type ' str ' >
>>> b = eval(a)
>>> print b
([1, 2], [3, 4], [5, 6], [7, 8], (9, 0))
>>> type(b)
<type 'tuple'>

Original address:

https://www.cnblogs.com/liu-shuai/p/6098246.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325270190&siteId=291194637