python重要函数eval多种用法解析
作者:小小程序员ol 时间:2023-02-08 20:16:46
这篇文章主要介绍了python重要函数eval多种用法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
1.参数会作为一个 Python 表达式(从技术上说是一个条件列表)被解析并求值
>>> x = 1
>>> eval('x+1')
2
2.去除字符串两边的引号
>>> 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'}
来源:https://www.cnblogs.com/python960410445/p/12150451.html
标签:python,函数,eval
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
解决golang 反射interface{}做零值判断的一个重大坑
2024-05-21 10:24:27
![](https://img.aspxhome.com/file/2023/8/123958_0s.png)
SQL Server手工插入标识列的方法
2024-01-27 05:41:35
flask-script模块的具体使用
2022-04-08 10:53:34
ASP.NET教程第二讲:安装ASP.NET
2007-08-07 11:59:00
让SQL Server数据库自动执行管理任务(一)
2009-03-20 10:35:00
MySQL数据库优化之分表分库操作实例详解
2024-01-20 10:33:53
nx.adjacency_matrix计算邻接矩阵与真实结果不一致的解决
2023-12-06 14:23:56
Python @property原理解析和用法实例
2023-05-19 00:59:41
Sql server 2005 找出子表树
2008-11-24 15:23:00
pytorch实现好莱坞明星识别的示例代码
2022-11-28 14:26:47
![](https://img.aspxhome.com/file/2023/7/96957_0s.jpg)
pytest内置fixture使用临时目录流程详解
2021-12-27 06:49:23
PHP之mysql位运算案例讲解
2023-06-13 06:16:19
![](https://img.aspxhome.com/file/2023/2/55552_0s.png)
用AspJpeg调整文字水印透明,生成图片水印的效果
2008-12-29 19:43:00
![](https://img.aspxhome.com/file/UploadPic/200812/29/20061218144728479-19s.jpg)
DelphiXE连接MySQL5.1
2010-12-08 16:44:00
Python自动化办公之群发邮件案例详解
2023-09-08 17:10:36
![](https://img.aspxhome.com/file/2023/9/120929_0s.jpg)
pycharm安装图文教程
2022-01-14 08:43:42
![](https://img.aspxhome.com/file/2023/7/109337_0s.png)
原生js仿浏览器滚动条效果
2024-04-10 16:09:33
![](https://img.aspxhome.com/file/2023/7/136787_0s.png)
python 按照sheet合并多个Excel的示例代码(多个sheet)
2022-07-13 05:20:20
![](https://img.aspxhome.com/file/2023/9/130619_0s.png)
解决python3 中的np.load编码问题
2021-12-19 20:28:27
用Python编写一个每天都在系统下新建一个文件夹的脚本
2021-11-08 22:39:16