python练习题_04
import os def fetch(data):
# print('\033[1;43m这是查询功能\033[0m')
# print('\033[1;43m用户数据是\033[0m',data)
backend_data='backend %s'%data
with open('website.conf','r',encoding='utf-8') as read_f:
tag = False
ret = []
for read_line in read_f:
if read_line.strip()==backend_data:
tag=True
continue
if tag and read_line.startswith('backend'):
break
if tag:
print(read_line,end='')
ret.append(read_line)
return ret def add():
pass def change(data):
print('这是修改功能')
backend= data[0]['backend']
backend_data='backend %s'%backend
old_sever_record='%s sever %s weight %s\n'%(' '*8,data[0]['record']['sever'],
data[0]['record']['weight'])
new_sever_record='%s sever %s weight %s\n'%(' '*8,data[1]['record']['sever'],
data[1]['record']['weight']) res=fetch(backend)
print('change函数',res) # if not res or old_sever_record not in res:
# return '你要修改的记录不存在'
# else:
index=res.index(old_sever_record)
res[index]=new_sever_record
res.insert(0,'%s\n'%backend_data)
with open('website.conf','r') as read_f,\
open('website_new.conf','w') as write_f:
tag=False
has_wirte=False
for read_line in read_f:
if read_line.strip()==backend_data:
tag=True
continue
if tag and read_line.startswith('backend'):
tag=False
if not tag:
write_f.write(read_line)
else:
if not has_wirte:
for record in res:
write_f.write(record) def delete():
pass if __name__=='__main__':
msg='''
1.查询
2.添加
3.修改
4.删除
5.退出
'''
msg_dic={
'':fetch,
'':add,
'':change,
'':delete
} while True:
print(msg)
choice=input('请输入你的选项:').strip()
if not choice:continue
if choice==5:break
data=input('请输入你的数据:'.strip())
if choice!='':
data=eval(data)
res=msg_dic[choice](data)
print(res) # [{'backend':'www.newboy2.org','record':{'sever':'1.1.1.8','weight':80}},{'backend':'www.newboy2.org','record':{'sever':'1.0.0.8','weight':100}}]
python练习题_04的更多相关文章
- Python练习题 028:求3*3矩阵对角线数字之和
[Python练习题 028] 求一个3*3矩阵对角线元素之和 ----------------------------------------------------- 这题解倒是解出来了,但总觉得 ...
- Python练习题 027:对10个数字进行排序
[Python练习题 027] 对10个数字进行排序 --------------------------------------------- 这题没什么好说的,用 str.split(' ') 获 ...
- Python练习题 026:求100以内的素数
[Python练习题 026] 求100以内的素数. ------------------------------------------------- 奇怪,求解素数的题,之前不是做过了吗?难道是想 ...
- Python练习题 025:判断回文数
[Python练习题 025] 一个5位数,判断它是不是回文数.即12321是回文数,个位与万位相同,十位与千位相同. ---------------------------------------- ...
- Python练习题 024:求位数及逆序打印
[Python练习题 024] 给一个不多于5位的正整数,要求:一.求它是几位数,二.逆序打印出各位数字. ---------------------------------------------- ...
- Python练习题 004:判断某日期是该年的第几天
[Python练习题 004]输入某年某月某日,判断这一天是这一年的第几天? ---------------------------------------------- 这题竟然写了 28 行代码! ...
- Python练习题-1.使用匿名函数对1~1000求和,代码力求简洁。
Python 练习 标签(空格分隔): Python Python练习题 Python知识点 一.使用匿名函数对1~1000求和,代码力求简洁. 答案: In [1]: from functools ...
- PYTHON练习题 二. 使用random中的randint函数随机生成一个1~100之间的预设整数让用户键盘输入所猜的数。
Python 练习 标签: Python Python练习题 Python知识点 二. 使用random中的randint函数随机生成一个1~100之间的预设整数让用户键盘输入所猜的数,如果大于预设的 ...
- python 基础 2.8 python练习题
python 练习题: #/usr/bin/python #coding=utf-8 #@Time :2017/10/26 9:38 #@Auther :liuzhenchuan #@File ...
随机推荐
- Redis连接方式
连接redis 本地安装了Redis并运行在6379端口,密码设置为 foobared. 1. from redis import StrictRedis redis = StrictRedis(ho ...
- mini-treeselect的动态赋值
<div id="faultTree" allowdrag="false" allowdrop="true" class=" ...
- CAFFE在win10+VS2017下的安装笔记
老版的caffe在BVLC的github上已经找不到,如果要想下载老版caffe可以下载微软的caffe版本:https://github.com/Microsoft/caffe 网上的大多安装caf ...
- 【Dubbo源码学习】负载均衡算法(1)-随机算法
/** * random load balance. * */public class RandomLoadBalance extends AbstractLoadBalance { public s ...
- 【Jquery Mobile教程】【问题】- 在页面切换时会闪烁
用jQuery mobile开发移动应用和web应用的时候,页面切换会闪烁. 感觉,造成这样的原因大概是jQuery mobile在移动浏览器下的性能不佳造成的.jQuery mobile页面切换可以 ...
- Elasticsearch-6.7.0系列(三)5601端口 kibana——ES的UI界面
https://artifacts.elastic.co/downloads/kibana/kibana-6.7.0-linux-x86_64.tar.gz Kibana下载 有了elast ...
- Linux 系统报错 rcu_preempt detected stalls on CPUs/tasks
说在前面的一些废话: 这是什么错误我不知道,为什么出现我不知道! 那为什么还要把他写出来了,只是因为这个错误遇到了,而且浪费了我很多时间和精力. 故事留给自己看,解决办法就是,重新升级一下Linux系 ...
- 优化读取纯真IP数据库QQWry.dat获取地区信息
改自HeDaode 2007-12-28的代码 将之改为从硬盘读取后文件后,将MemoryStream放到内存中,提高后续查询速度 ///<summary> /// 提供从纯真IP数据库搜 ...
- idea springboot 父子工程 子工程maven不自动import
父工程删除对spring boot启动项的引用,因为父工程 dependencyManagement,它不会自动加载包,只指定包的版本, 如果在父工程中引用了包,但是没有指定包版本,子工程将不会识别到 ...
- Windows下搭建Python虚拟环境
一.安装使用 virtualenv 1. 安装 virtualenv pip install virtualenv 2. 新建虚拟环境 virtualenv env1 用 Python3 创建虚拟环境 ...