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

Python之使用eval()函数将字符串的数据结构提取出来

时间:2019-05-06 19:23:44      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:span   ldb   用户输入   %s   max   NPU   rip   new   字符串   

data = input(请输入你要修改的对象:).strip()
‘‘‘
输入下面的字典列表
[{‘backend‘:‘www.oldboy1.org‘,‘record‘:{‘server‘:‘2.2.2.4‘,‘weight‘:20,‘maxconn‘:3000}},{‘backend‘:‘www.oldboy1.org‘,‘record‘:{‘server‘:‘2.2.2.5‘,‘weight‘:30,‘maxconn‘:4000}}]
‘‘‘
print(data)
data = eval(data)
# 如果不使用该项命令,会报错:string indices must be integers
# 作用:把用户输入的字符串里的数据结构提取出来
print(data)
print(data[0][backend])
old_server_record = %sserver %s %s weight %s maxconn %s\n %( *8,data[0][record][server],
                                                                data[0][record][server],
                                                                data[0][record][weight],
                                                                data[0][record][maxconn])
print(old_server_record)
new_server_record = %sserver %s %s weight %s maxconn %s\n % (  * 8, data[1][record][server],
                                                                 data[1][record][server],
                                                                 data[1][record][weight],
                                                                 data[1][record][maxconn])
print(new_server_record)

 

Python之使用eval()函数将字符串的数据结构提取出来

标签:span   ldb   用户输入   %s   max   NPU   rip   new   字符串   

原文地址:https://www.cnblogs.com/lzn-2018/p/10821108.html

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