1 1,Alex Li,22,13651054608,IT,2013-04-01 2 2,Jack Wang,30,13304320533,HR,2015-05-03 3 3,Rain Liu,25,1383235322,Saies,2016-04-22 4 4,Mack Cao,40,1356145343,HR,2009-03-01
xinxi.txt
1 # _*_ coding:utf-8 _*_ 2 staff_table = [] 3 def file_to_date(file_path): 4 global staff_table 5 staff_table=[{‘staff_id‘:int(line.strip().split(‘,‘)[0]),‘name‘:line.strip().split(‘,‘)[1],‘age‘:int(line.strip().split(‘,‘)[2]),‘phone‘:line.strip().split(‘,‘)[3],‘dept‘:line.strip().split(‘,‘)[4],‘enroll_date‘:line.strip().split(‘,‘)[5]} for line in open(file_path,encoding=‘utf-8‘)] 6 def output(line,cmd): 7 for key in line: 8 if key in cmd[0].split(‘,‘) or cmd[0] == ‘*‘: 9 print(line[key], end=‘ ‘) 10 print() 11 def select(): 12 ‘‘‘ 13 select name,age from staff_table where age > 22 14 select * from staff_table where dept = "IT" 15 select * from staff_table where enroll_date like "2013" 16 显示查询的条数 17 ‘‘‘ 18 while True: 19 n=0 20 file_to_date(‘xinxi.txt‘) 21 cmd=input(‘please input cmd:‘).strip().lower().split() 22 cmd=[cmd[1], cmd[5:]] 23 if ‘\‘‘in cmd[1][2]:cmd[1][2]=cmd[1][2].strip(‘\‘‘) 24 elif ‘\"‘in cmd[1][2]:cmd[1][2]=cmd[1][2].strip(‘\"‘) 25 for line in staff_table: 26 if cmd[1][1] == ‘>‘and line[cmd[1][0]] > int(cmd[1][2]): 27 output(line, cmd) 28 n += 1 29 if cmd[1][1] == ‘=‘and (line[cmd[1][0]] in [cmd[1][2] ,cmd[1][2].strip(‘\‘‘),cmd[1][2].strip(‘\"‘),cmd[1][2].lower()] or (cmd[1][2].isdigit() and line[cmd[1][0]] == int(cmd[1][2]))): 30 output(line,cmd) 31 n += 1 32 if cmd[1][1] == ‘like‘and cmd[1][2] in line[cmd[1][0]].split(‘-‘) or cmd[1][2] in line[cmd[1][0]] or cmd[1][2] in line[cmd[1][0]].lower(): 33 output(line,cmd) 34 n += 1 35 print(‘相关查询共%d条‘ % n) 36 select()
查询功能
时间: 2024-12-10 15:02:57