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,"-"))
时间: 2024-11-03 01:55:10

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

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

有点小bug,还待改进,代码如下: 1.查询: 输入:www.ordboy1.org 1 list = [] #创建一个空列表 2 flag = False #设置一个开关 3 input1 = input("plesae your input "www.oldboy1.org" :") 4 with open("haproxy.conf",encoding="utf-8") as f_read: #把源文件以utf-8格式打

IOS9中联系人对象的使用及增删改查操作的函数封装

之前克服重重困难把IOS9中新的类联系人搞明白了,现在把增删改查封装成了函数,如下: 1 // 2 // ViewController.m 3 // IOS9中联系人CNContact的使用 4 // 5 // Created by mac on 16/5/6. 6 // Copyright © 2016年 mzw. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 11 //导入IOS9联系人模型 12 @impo

python实现mongodb的增删改查

环境: 192.168.122.1    python 192.168.122.11  mongodb 一.安装pip,python的管理工具 官网下载: pip-18.0 安装pip [[email protected] pip-18.0]# python setup.py install 如下,安装成功 注:在安装pip时,下载setuptools-39.2.0.zip 解压并运行python setup.py install ,否则后面安装会有错误: 二.python连接mongodb P

Python3.5 day4作业:对员工信息文件,实现增删改查操作。

需求: 1.可进行模糊查询,语法至少支持下面3种: 1.select name,age from staff_table where age > 22 2.select  * from staff_table where dept = IT 3.查到的信息,打印后,最后面还要显示查到的条数 2.可创建新员工纪录,以phone做唯一键,staff_id需自增 3.可删除指定员工信息纪录,输入员工id,即可删除 4.可修改员工信息,语法如下: 1.UPDATE staff_table SET dep

Python学习之路:文件操作之增删改查

f = open("yesterday","r",encoding="utf-8") #print(f.read()) #for i in range(5): # print(f.readline()) #打印前5行 #low loop ''' for index,line in enumerate(f.readline()): if index == 9: print('---------我是分割线---------') continue pr

python中字典的增删改查及相关知识点

1 ''' 2 数据划分:可变数据类型,不可变数据类型 3 不可变数据类型:tuple,str,bool,int 可哈希 4 可变数据类型: list, dict, set 不可哈希 5 dict key:必须是不可变数据类型 可哈希 6 dict value: 任意数据类型 7 dict 优点:二分法去查找 8 存储大量关系型数据 9 特点: 无序的 10 ''' 11 12 #>>>增 13 14 #使用key增加,类似于list的索引改变值 15 #但是list中的索引必须在范围内

Python3练习:对员工信息文件,实现增删改查操作

1.练习要求: 2.数据文件(data_staff.txt) 1,Alex Li,22,13651054684,运维,2013-02-04 2,Jack Wang,20,13312331232,HR,2014-06-03 3,Mike Cao,20,15504231232,Sales,2013-05-06 4,Jack Chen,34,12404231232,HR,2011-02-01 5,Lu Haojie,21,15204231232,运维,2013-08-12 3.实现代码 A.主程序部分

python学习-字典的增删改查

1 dic1 = {'name':'paoche', 2 'age':19, 3 'active':['beijing','hebei'], 4 'id':{'wangyi':'skmdod008b', 5 'sina':'skmdod00bc' 6 } 7 } 一.字典的增 方法1:直接新增键值对,若无此键则新增键值对,有此键则修改值 1 dic1['high'] = 180 #没有键值对,则追加 2 dic1['age'] = 30 #有键值,则修改 3 print(dic1) 4 "&qu

文件操作之增删改查

对文件只读取一行readline() 如果我们想读取前5行: readlines()可以把文件读取为列表,每个元素为一行. 如果我们想循环文件一行一行读,到第10行不打印,显示分隔符: 这个方法不好,如果我们文件有20个G的文件,这个readlines()速度太慢,不合适! 我们必须一行一行的读,每次在内存里只保存一行.(readline()也不行,一行一行还是读到内存了),用如下的这个方法最好,每次在内存里只保留了一行数据!这个效率最高!在这里f是迭代器,并不是列表了! 用下面的的循环,来实现