1.参数会作为一个 Python 表达式(从技术上说是一个条件列表)被解析并求值

>>> x = 1
>>> eval('x+1')
2

2.去除字符串两边的引号

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
>>> a='"srting"'
>>> print(a)
"srting"
>>> b=eval(a)
>>> print(b)

srting
也可以用

>>> a.strip('"')
'srting'

3.字符串转字典

>>> a= "{'name':'linux','age':18}"
>>> type(a)
<type 'str'>
>>> b=eval(a)
>>> b
{'age': 18, 'name': 'linux'}
>>> type(b)
<type 'dict'>

4.传递全局变量

>>> a= "{'name':'linux','age':age}"
>>> b=eval(a,{"age":1822})
>>> b
{'age': 1822, 'name': 'linux'}
>>> type(b)
<type 'dict'>

5.传递本地变量

>>> a= "{'name':'linux','age':age}"
>>> age=18
>>> b=eval(a,{"age":1822},locals())
>>> b
{'age': 18, 'name': 'linux'}
扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄