python中的str()与eval函数
author:headsen chen
date:2018-04-09 10:48:22
eval函数是把str转化成list、dict、tuple
str函数把list,dict,tuple转为为字符串
----------------------------------------------------------------
# 字符串转换成列表
a = "[[1,2], [3,4], [5,6], [7,8], [9,0]]"
print(type(a))
b = eval(a)
print(b,type(b))
<class 'str'>
([1, 2], [3, 4], [5, 6], [7, 8], [9, 0]) <class 'tuple'>
-------------------------------------------------------------
# 字符串转换成字典
a = "{1: 'a', 2: 'b'}"
print(type(a))
b = eval(a)
print(b,type(b))
<class 'str'>
{1: 'a', 2: 'b'} <class 'dict'>
----------------------------------------------------------
# 字符串转换成元组
a = "([1,2], [3,4], [5,6], [7,8], (9,0))"
print(type(a))
b=eval(a)
print(b,type(b))
<class 'str'>
([1, 2], [3, 4], [5, 6], [7, 8], [9, 0]) <class 'tuple'>