在python中,一般都是使用eval,不过这个表达式不够安全。今天在stackoverflow上看到有人说ast这个模块

查了一下

ast.literal_eval(node_or_string)
Safely evaluate an expression node or a Unicode or Latin-1 encoded string containing a Python expression. The string or node provided may only consist of the following Python literal structures: strings, numbers, tuples, lists, dicts, booleans, and None.

This can be used for safely evaluating strings containing Python expressions from untrusted sources without the need to parse the values oneself.

可以实现安全的代码转换,使用起来很简单


import ast
userAnser = ast.literal_eval([1,2,3,4])


更多相关文章

  1. Python/模块与包之模块
  2. Python多行正则表达式忽略字符串中的n行
  3. 从正则表达式中浏览和提取字符类
  4. python-布尔表达式
  5. eclipse调用python模块是出错及解决
  6. python3 爬虫之requests模块使用总结
  7. python正则表达式的使用
  8. 面试---Python中的模块和包是什么?
  9. python及其模块下载集合

随机推荐

  1. 系统安装经历
  2. 简单搭建syslog-ng server记录log
  3. 美丽新世界:linux 下的惬意生活
  4. Linux记录-HDFS副本机制
  5. Linux curl 命令模拟 POST/GET 请求
  6. 检查进程是否仍在运行
  7. LTP--linux稳定性测试 linux性能测试 ltp
  8. Linux Shell编程参考大全
  9. 高级Linux工程师常用软件清单
  10. linux系统更改目录和文件的权限总结