码迷,mamicode.com
首页 > 编程语言 > 详细

python-eval、repr函数

时间:2017-04-03 18:05:05      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:str   字符   repr   元组   document   函数   mda   字符串转换   ==   

eval函数将字符串当成有效Python表达式来求值,并返回计算结果 

string+  eval==》list,tuple,dict

repr + list,tuple,dict ==》string
#################################################
字符串转换成列表
>>>a = "[[1,2], [3,4], [5,6], [7,8], [9,0]]"
>>>type(a)
<type ‘str‘>
>>> b = eval(a)
>>> print b
[[1, 2], [3, 4], [5, 6], [7, 8], [9, 0]]
>>> type(b)
<type ‘list‘>
#################################################
字符串转换成字典
>>> a = "{1: ‘a‘, 2: ‘b‘}"
>>> type(a)
<type ‘str‘>
>>> b = eval(a)
>>> print b
{1: ‘a‘, 2: ‘b‘}
>>> type(b)
<type ‘dict‘>
#################################################
字符串转换成元组
>>> a = "([1,2], [3,4], [5,6], [7,8], (9,0))"
>>> type(a)
<type ‘str‘>
>>> b = eval(a)
>>> print b
([1, 2], [3, 4], [5, 6], [7, 8], (9, 0))
>>> type(b)
<type ‘tuple‘>

 

 

 

---------------------------------

Python Cookbook 3rd Edition Documentation — python3-cookbook 2.0.0 文档
http://python3-cookbook.readthedocs.io/zh_CN/latest/

python-eval、repr函数

标签:str   字符   repr   元组   document   函数   mda   字符串转换   ==   

原文地址:http://www.cnblogs.com/wanghui626/p/6662341.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!