函数文件操作实现数据增删改查---low版本
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了函数文件操作实现数据增删改查---low版本相关的知识,希望对你有一定的参考价值。
首先说明这个脚本很low,目前水平有限,只能实现使用固定的语法对数据进行增删改查。但是现在脚本不low,怎么让明年的我来嘲笑今年的自己
需求 a.可进行模糊查询,语法至少支持下面3种: select name,age from staff_table where age > 22 select * from staff_table where dept = "IT" select * from staff_table where enroll_date like "2013" 查到的信息,打印后,最后面还要显示查到的条数 b.可创建新员工纪录,以phone做唯一键,staff_id需自增,语法如下: insert lyndon 24 13811688803 OP 20017-07-01 c.可删除指定员工信息纪录,输入员工id,即可删除,语法如下: delete 删除序号(1-4) d.可修改员工信息,语法如下: UPDATE staff_table SET dept="Market" WHERE where dept = "IT" 注意:以上需求,要充分使用函数,请尽你的最大限度来减少重复代码!
3、测试信息 1,Alex Li,22,13651054608,IT,2013-04-01 2,Jack Wang,30,13304320533,HR,2015-05-03 3,Rain Liu,25,1383235322,Sales,2016-04-22 4,Mack Cao,40,1356145343,HR,2009-03-01
def mem(): print(‘‘‘ ---------------------------使用说明------------------------------ 查询语法: select name,age from staff_table where age > 22 select * from staff_table where dept = "IT" select * from staff_table where enroll_date like "2013" 创建语法: insert lyndon 24 13811688803 OP 20017-07-01 删除语法: delete 序号(序号初始范围1-4) 修改语法: UPDATE staff_table SET dept="Market" WHERE where dept = "IT" -----------------水平有限,只能满足以上语法---------------------- 【exit】 退出 ‘‘‘) def check_sql(sql): parse_sql={ ‘insert‘:insert, ‘delete‘:delete, ‘update‘:update, ‘select‘:select } sql_l=sql.split(‘ ‘) func=sql_l[0] res=‘‘ if func in parse_sql: res=parse_sql[func](sql_l) return res # print(sql_l) def select(sql_l): #[‘select‘, ‘name,age‘, ‘from‘, ‘staff_table‘, ‘where‘, ‘age‘, ‘>‘, ‘22‘] count=0 if ‘age‘ in sql_l: with open(‘info.txt‘,‘r‘,encoding=‘utf-8‘) as f_read: for line in f_read: info_l=line.split(‘,‘) if int(info_l[2]) > 22: count+=1 print(line) if ‘dept‘ in sql_l: with open(‘info.txt‘,‘r‘,encoding=‘utf-8‘) as f_read: for line in f_read: info_l=line.split(‘,‘) if info_l[4] == ‘IT‘: count+=1 print(line) if ‘like‘ in sql_l: with open(‘info.txt‘,‘r‘,encoding=‘utf-8‘) as f_read: for line in f_read: info_l=line.split(‘,‘) if info_l[-1].split(‘-‘)[0] == ‘2013‘: count += 1 print(line) print(‘共查询到 %s 条信息‘ %count) def update(sql_l): #[‘update‘, ‘staff_table‘, ‘set‘, ‘dept="market"‘, ‘where‘, ‘where‘, ‘dept‘, ‘=‘, ‘"it"‘] with open(‘info.txt‘, ‘r+‘, encoding=‘utf-8‘) as f_read: lines=f_read.readlines() with open(‘info.txt‘,‘w‘,encoding=‘utf-8‘) as f_write: for line in lines: if ‘IT‘ in line: line=line.replace(‘IT‘,‘Market‘,1) print(‘信息已更新 %s‘ %line) f_write.write(line) def delete(sql_l): import os with open(‘info.txt‘, ‘r‘, encoding=‘utf-8‘) as f_read, open(‘.info.swap.txt‘,‘w+‘,encoding=‘utf-8‘) as f_write: for line in f_read: info_l = line.split(‘,‘) if sql_l[1] == info_l[0] : print(‘%s 此信息已删除‘ %line) else: f_write.write(line) os.remove(‘info.txt‘) os.rename(‘.info.swap.txt‘,‘info.txt‘) def insert(sql_l): #[‘insert‘, ‘lyndon‘, ‘24‘, ‘13811688803‘, ‘op‘, ‘20017-07-01‘] phone=[] with open(‘info.txt‘, ‘r‘, encoding=‘utf-8‘) as f_read: for line in f_read: info_l = line.split(‘,‘) count=int(info_l[0]) phone.append(info_l[3]) if sql_l[3] not in phone: count+=1 sql_l[0]=str(count) string=‘,‘.join(sql_l) with open(‘info.txt‘, ‘a‘, encoding=‘utf-8‘) as f: f.write(‘\n‘+string) print(‘%s 信息已插入‘ %string) else: print(‘此信息已存在‘) while True: print(‘输入help查看帮助‘) sql=input(‘sql>>>‘).strip().lower() if len(sql) == 0:continue if sql == ‘exit‘:break if sql == ‘help‘: mem() im=check_sql(sql)
本文出自 “lyndon” 博客,请务必保留此出处http://lyndon.blog.51cto.com/11474010/1949364
以上是关于函数文件操作实现数据增删改查---low版本的主要内容,如果未能解决你的问题,请参考以下文章