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格式打开到f_read
 5     for line in f_read:  #遍历文件到line这个变量中
 6         if line.startswith("backend") and input1 in line:
 7             flag = True
 8             continue  #跳出本层循环
 9
10         if line.startswith("backend"): #判断字符串是否以backend开头
11             break
12         if flag:
13             list.append(line.strip()) #追加到空列表
14 print("hello".center(50,"-"))
15
16 for i in list:  #遍历列表
17     print(i)

2.删除:

输入:{"backend":"www.oldboy20.org","record":{"server":1111,"weight":2222,"maxconn":3333}}

 1 choice = input("please your input  is {} :")
 2     # {"backend":"www.oldboy20.org","record":{"server":1111,"weight":2222,maxconn:3333}}
 3     dic = eval(choice)  # 把输入的字符串转成字典
 4     tltle = dic["backend"]
 5     tetle1 = dic["record"]  #这个没有用
 6     with open("haproxy.conf", encoding="utf-8") as f_read, open("test5", "w", encoding="utf-8") as w_write:
 7
 8         for line in f_read:
 9             w_write.write(line)
10             if line.startswith("backend") and tltle in line:
11                 # w_write.write(line)
12                 break

3.增加:

输入:{"backend":"www.oldboy20.org","record":{"server":1111,"weight":2222,"maxconn":3333}}

 1 choice = input("please your input is {}  : ")
 2     # {"backend":"www.oldboy20.org","record":{"server":1111,"weight":2222,"maxconn":3333}}
 3     dic = eval(choice)  # 把字符串转成字典
 4     title = dic[‘backend‘]  # "www.oldboy20.org"
 5     record = dic[‘record‘]
 6     r_s = ‘        server %s weight %s maxconn %s\n‘  7           % (record[‘server‘], record[‘weight‘], record[‘maxconn‘])
 8
 9     with open("haproxy.conf", encoding="utf-8") as f_read, open("test3", mode="w", encoding="utf-8") as w_read:
10         for line in f_read:
11             w_read.write(line)
12             if line.startswith(‘backend‘) and title in line:
13                 # print(‘11111>‘)
14                 w_read.write(r_s)
15                 # w_read.write(‘=====>‘)
16         w_read.flush()

4.修改:

输入:{"backend":"www.oldboy20.org","record":{"server":1111,"weight":2222,"maxconn":3333}}

 1 choice = input("please your input is {} :")
 2     # {"backend":"www.oldboy20.org","record":{"server":1111,"weight":2222,"maxconn":3333}}
 3     dic = eval(choice)  #把输入的字符串转成字典
 4     tltel = dic["backend"]
 5     tltel1 = dic["record"]
 6     dic["record"]["server"] = "0000"
 7     dic["record"]["weight"] = "0000"
 8     dic["record"]["maxconn"] = "0000"
 9     # print(dic)
10     #格式化字符串
11     r_s = "        server %s weight %s maxconn %s" % (tltel1["server"], tltel1[‘weight‘], tltel1["maxconn"])
12     with open("haproxy.conf", encoding="utf-8") as f_read, open("test6", mode="w", encoding="utf-8") as w_write:
13         for line in f_read:
14             w_write.write(line) # 把line中的字符串写到w_write中
15             if line.startswith("backend") and tltel in line: #判断 line中字符串否以“backend”开头,并且满不满足tetle在这行
16                 w_write.write(r_s)  #把字符串写入到w_rite中
17                 break   #跳出循环
时间: 2024-11-09 00:17:23

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

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是迭代器,并不是列表了! 用下面的的循环,来实现

文件操作之增删改查2以及文件打开的其他一些模式

read()会让你读取的光标变成最后. tell()把你现在文件的句柄的指针打印出来.文件的开头指针位置是0 72的意思代表字符的个数.下面的例子,只读取5个字符串个数: 如果你想把光标移回去,移动到首位f.seek(0) f.detach()在文件还没关闭之前,把文件编码从GBK改成UTF-8 f.encoding() 打印打开文件的编码:之前文件是用UTF-8打开的,所以这里显示UTF-8,注意并不是文件文件的存储编码是啥. f.fileno()返回文件的编号,windows有一个专门的接口

Python操作mysql(增删改查)

#!/usr/bin/env python #coding:utf-8 import MySQLdbtry:         #连接mysql的方法:connect('ip','user','password','dbname')         #conn=MySQLdb.connect(host='localhost',user='root',passwd='123456',db='test')     conn =MySQLdb.connect('127.0.0.1','root','12