发布于2019-08-06 10:07 阅读(751) 评论(0) 点赞(4) 收藏(5)
import os def file_handler(backend_data,res=None,type='fetch'): # 查询功能 if type == 'fetch': with open('test_new.txt','r') as read_f: ret = [] tag = False 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 if type == 'change': # 修改功能 with open('test_new.txt', 'r') as read_f, \ open('test_new.txt_new', 'w') as write_f: tag = False has_write = 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_write: for record in res: write_f.write(record) has_write = True os.rename('test_new.txt', 'test_new.txt.bak') os.rename('test_new.txt_new', 'test_new.txt') os.remove('test_new.txt.bak') def fetch(data): print('您输入的信息是:',data) backend_data= 'backend %s' % data return file_handler(backend_data) def change(data): print('开始修改功能') print('用户输入的信息是:',data) backend=data[0]['backend'] backend_data='backend %s' %backend old_server_record ='%s server %s %s\n'%(*9,data[0]['record']['server']) new_server_record = '%s server %s %s\n' % (*9, data[1]['record']['server']) print('您想要修改的记录:',old_server_record) res=fetch(backend) print(res) if not res or old_server_record not in res: return '您要修改的信息不存在' else: index = res.index(old_server_record) res[index]=new_server_record res.insert(0,'%s\n' %backend_data) file_handler(backend_data,res=res,type='change') if __name__== '__main__': msg=''' 1:查询 2:修改 5:退出 ''' msg_dic={ '1':fetch, '2':change } while True: print(msg) choose = input('请输入您要的选项:').strip() if not choose: continue if choose == '5':break data = input('请输入您的信息:').strip() if choose != '1': data = eval(data) res = msg_dic[choose](data) # 查询到的结果放到一个列表里输出 print('最终的结果:',res)
作者:妖怪还我师傅
链接:https://www.pythonheidong.com/blog/article/7467/63feb94c501f56a5361e/
来源:python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!