1data = input('请输入你要修改的对象:').strip() 2''' 3输入下面的字典列表 4[{'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}}] 5''' 6print(data) 7data = eval(data) 8# 如果不使用该项命令,会报错:string indices must be integers 9# 作用:把用户输入的字符串里的数据结构提取出来 10print(data) 11print(data[0]['backend']) 12old_server_record = '%sserver %s %s weight %s maxconn %s\n' %(' '*8,data[0]['record']['server'], 13 data[0]['record']['server'], 14 data[0]['record']['weight'], 15 data[0]['record']['maxconn']) 16print(old_server_record) 17new_server_record = '%sserver %s %s weight %s maxconn %s\n' % (' ' * 8, data[1]['record']['server'], 18 data[1]['record']['server'], 19 data[1]['record']['weight'], 20 data[1]['record']['maxconn']) 21print(new_server_record)
# 注意:eval 对数据类型的提取有局限性,一般建议使用 json模块里面的 json.loads() 进行提取