首页 > 代码库 > python--------文件处理之增删改查--------作业用函数

python--------文件处理之增删改查--------作业用函数

1,刚学到函数,用的还不熟练,只能以简单的函数调用来实现,循环这部分还每想到这么用函数来实现。

2,  还是有bug。

3,代码:

  1 def add():
  2     ‘‘‘
  3     添加一条语句
  4 
  5     ‘‘‘
  6 
  7     choice = input("please your input is {}  : ")
  8     # {"backend":"www.oldboy20.org","record":{"server":1111,"weight":2222,"maxconn":3333}}
  9     dic = eval(choice)  # 把字符串转成字典
 10     title = dic[backend]  # "www.oldboy20.org"
 11     record = dic[record]
 12     r_s =         server %s weight %s maxconn %s\n  13           % (record[server], record[weight], record[maxconn])
 14 
 15     with open("haproxy.conf", encoding="utf-8") as f_read, open("test3", mode="w", encoding="utf-8") as w_read:
 16         for line in f_read:
 17             w_read.write(line)
 18             if line.startswith(backend) and title in line:
 19                 # print(‘11111>‘)
 20                 w_read.write(r_s)
 21                 # w_read.write(‘=====>‘)
 22         w_read.flush()
 23 
 24 
 25 
 26 def search():
 27     ‘‘‘
 28     查询
 29      
 30     ‘‘‘
 31 
 32     list = []
 33     flag = False
 34     input1 = input("plesae input ‘www.oldboy1.org‘ :")
 35     with open("haproxy.conf", encoding="utf-8") as f_read:
 36         for line in f_read:
 37             if line.startswith("backend") and input1 in line:
 38                 flag = True
 39 
 40                 continue
 41 
 42             if line.startswith("backend"):
 43                 break
 44             if flag:
 45                 list.append(line.strip())
 46     print("hello".center(50, "-"))
 47 
 48     for i in list:
 49         print(i)
 50 
 51 
 52 
 53 def delete():
 54     ‘‘‘
 55     删除
 56     
 57     ‘‘‘
 58 
 59     choice = input("please your input  is {} :")
 60     # {"backend":"www.oldboy20.org","record":{"server":1111,"weight":2222,maxconn:3333}}
 61     dic = eval(choice)  # 把输入的字符串转成字典
 62     tltle = dic["backend"]
 63     tetle1 = dic["record"]  #这个没有用
 64     with open("haproxy.conf", encoding="utf-8") as f_read, open("test5", "w", encoding="utf-8") as w_write:
 65 
 66         for line in f_read:
 67             w_write.write(line)
 68             if line.startswith("backend") and tltle in line:
 69                 # w_write.write(line)
 70                 break
 71 
 72 
 73 def change():
 74     ‘‘‘
 75     修改一条语句
 76      
 77     ‘‘‘
 78 
 79     choice = input("please your input is {} :")
 80     # {"backend":"www.oldboy20.org","record":{"server":1111,"weight":2222,"maxconn":3333}}
 81     dic = eval(choice)  #把输入的字符串转成字典
 82     tltel = dic["backend"]
 83     tltel1 = dic["record"]
 84     dic["record"]["server"] = "0000"
 85     dic["record"]["weight"] = "0000"
 86     dic["record"]["maxconn"] = "0000"
 87     # print(dic)
 88     #格式化字符串
 89     r_s = "        server %s weight %s maxconn %s" % (tltel1["server"], tltel1[weight], tltel1["maxconn"])
 90     with open("haproxy.conf", encoding="utf-8") as f_read, open("test6", mode="w", encoding="utf-8") as w_write:
 91         for line in f_read:
 92             w_write.write(line) # 把line中的字符串写到w_write中
 93             if line.startswith("backend") and tltel in line: #判断 line中字符串否以“backend”开头,并且满不满足tetle在这行
 94                 w_write.write(r_s)  #把字符串写入到w_rite中
 95                 break   #跳出循环
 96 
 97 
 98 def can_msg():
 99     ‘‘‘
100     菜单
101      
102     ‘‘‘
103     msg = ‘‘‘
104      1,search: 查询
105      2,add   : 添加
106      3,delete: 删除
107      4,change: 修改
108      0,exit(0):退出
109     
110     ‘‘‘
111     print(msg)
112 
113 
114 while True :
115     can_msg()
116     choice = int(input("please your input choice  or exit(0):"))
117 
118     if choice == 0 :
119         print("good bye".center(50,"-"))
120         print("再_见".center(50,"-"))
121         exit()
122     if choice == 1:
123         search()
124     if choice == 2:
125         add()
126     if choice == 3:
127         delete()
128     if choice == 4:
129         search()
130                     # if choice == str(choice):
131                     #     print("请输入数字 谢谢 ".center(50,"-"))
132                         #continue
133     else:
134          print("请选择序列号 : 0-4".center(50,"-"))

 

python--------文件处理之增删改查--------作业用函数