码迷,mamicode.com
首页 > 其他好文 > 详细

字符串转化为字典,eval的简介

时间:2019-06-06 12:29:02      阅读:84      评论:0      收藏:0      [点我收藏+]

标签:int   res   执行   ict   str1   eval   app   登陆   转化   

字符串必须要有明确的特点,不然python也无法进行分割,思想就是将字符
串先转化为列表,然后向空列表里追加,最后再转化为字典。
def get_webinfo():
str1 = []
config = "a=登陆 b=注销"
result = config.split(‘ ‘)
for d in result:
str1.append(d.split("="))
print(dict(str1))

eval()函数很强大,可以直接将你所运行的代码进行python执行,
比如说print(eval("1+2"))可以得到结果3,
也可以进行变量的传递print(eval("{‘name‘:‘linux‘,‘age‘:age}",{"age":1822}))
输出结果:{‘name‘: ‘linux‘, ‘age‘: 1822}
如果你的字符串直接是字典的形式
def get_dict():
config = "{‘a‘:‘登陆‘, ‘b‘:‘注销‘}"
print(eval(config))

字符串转化为字典,eval的简介

标签:int   res   执行   ict   str1   eval   app   登陆   转化   

原文地址:https://www.cnblogs.com/yichuncom/p/10984098.html

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