python 中 字符串转换为数组,字典或表达式
2014-01-25 23:24
543 查看
在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])
相关文章推荐
- python中数组,元组,字典和字符串之间的转换
- 用python正则表达式转换php数组到javascript字典
- python中数组,元组,字典和字符串之间的转换
- oc之字典排序(将字符串转换成数字排序) 把字典放在数组内进行输出 字典赋值
- iOS开发:字典(NSDictionary)和数组( NSArray)转换成字符串
- python2.7字典转换成json时中文字符串变成unicode的问题:
- python中列表,元组,字典,字符串相互转换
- python 使用 simplejson 将字符串转换成字典dict
- Python中将字符串转换成字典dict类型
- python列表,元组,字典和字符串之间的相互转换
- python将字符串转换成字典的几种方法
- python的字典、列表于字符串的互相转换
- Python字符串、元组、列表、字典互相转换的方法
- Python 中将字符串转换为字典的方法
- python json格式字符串转换为字典格式
- 【知识】Python中字符串、元组、列表、字典的互相转换
- python笔记4 - 索引,切片,字符编码转换函数,字符串的属性方法,字符串格式化表达式,占位符,基于字典的字符串格式化,循环语句
- Python实现字符串与数组相互转换功能示例
- python 将字符串转换成字典dict
- python 字符串与字典之间的转换