Part 2-1 employee info, part2-1 employee info

Source: Internet
Author: User

Part 2-1 employee info, part2-1 employee info

README:

1. add, delete, modify, and query the employee information table program:

1) fuzzy query is supported. The syntax must be at least three:
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"
Finally, the number of items found is displayed.
2). You can create new employee records and use phone as the unique key. staff_id must be auto-incrementing.
3). You can delete a specified employee information record and enter the employee id to delete it.
4) The employee information can be modified. The syntax is as follows:
UPDATE staff_table SET dept = "Market" WHERE dept = "IT"

Flowchart:

 

Code:

# Coding: utf8import sysimport redef select (staff, field): cmd = input ("cmd> "). strip () cmd = cmd. replace ('from', 'from') cmd = cmd. replace ('where', 'where') if '*' in cmd: for I in field. keys (): sys. stdout. write (str (I) + '') print ('') for line in staff: info_list = re. split (R', + ', line. strip ('\ n') returns _list = re. split (R' [,;] + ', cmd) f_index = pai_list.index ('from') search_field = pai_list [1: f_inde X] from_field = performance_list [f_index + 1] if from_field! = 'Staff _ table': print ('\ 033 [31; 1 mplease select 'staff _ table '... \ 033 [0m') break if 'where' not in exist _list: # The where condition does not exist. Display All view_list = [] for I in range (len (search_field )): if search_field [I] = '*': view_list = info_list [:] else: view_list.append (info_list [field. get (search_field [I]) else: print (','. join (view_list) else: # The where condition w_index = pai_list.index ('where') where_str = ''. join (ipv_li St [w_index + 1:]) if re. search (r 'like', where_str): sizeof = 'like' where_list = re. split (r 'like', where_str) else: sizeof = re. search (R' [=> <] + ', where_str ). group () where_list = re. split (R' [=> <] + ', where_str) where_field = where_list [0] value = where_list [1] if re. search (R' [\ '\ "] +', value): # contains the 'and" representation characters, and processes' and "value = value. replace ('\ "','') value = value. replace ('\ '','') else: value = float (value) # Print (where_str, where_field, sizeof, value) # condition judgment>, <, =, like if sizeof = '>': v = info_list [field. get (where_field)] if float (v)> value: view_list = [] for I in range (len (search_field): if search_field [I] = '*': view_list = info_list [:] else: view_list.append (info_list [field. get (search_field [I]) else: print (','. join (view_list) elif sizeof = '<': v = info_list [field. get (where_field)] If float (v) <value: view_list = [] for I in range (len (search_field): if search_field [I] = '*': view_list = info_list [:] else: view_list.append (info_list [field. get (search_field [I]) else: print (','. join (view_list) elif sizeof = ': v = info_list [field. get (where_field)] if field. get (where_field) = 2: v = float (v) if v = value: view_list = [] for I in range (len (search_field): if search_fi Eld [I] = '*': view_list = info_list [:] else: view_list.append (info_list [field. get (search_field [I]) else: print (','. join (view_list) elif sizeof = 'like': v = info_list [field. get (where_field)] if value in v: view_list = [] for I in range (len (search_field): if search_field [I] = '*': view_list = info_list [:] else: view_list.append (info_list [field. get (search_field [I]) else: print (','. join (view _ List) else: passdef add (staff): staff. sort () num = int (re. split (R', ', staff [-1]) [0]) + 1 phone = input ("phone :"). strip () for line in staff: if phone = line [3]: print ("already exists... ") return False if not re. match (R' ^ \ d + $ ', phone) or len (phone) <7: print ("format error... ") return False name = input (" name :"). strip () age = input ("age :"). strip () dept = input ("dept :"). strip () enroll_date = input ("Enroll_date :"). strip () for s in (name, age, dept, enroll_date): if not len (s): print ("input is null... ") return False else: staff. append ('% s, % s \ n' % (num, name, age, phone, dept, enroll_date )) return staffdef update (staff, field): cmd = input ("cmd> "). strip () up_list = [] cmd = cmd. replace ('set', 'set') cmd = cmd. replace ('where', 'where') for line in staff: info_list = re. split (R', + ', Line. strip ('\ n') into _list = re. split (R' [,;] +', cmd) if exist _list [1]! = 'Staff _ table': print ('\ 033 [31; 1 mplease update 'staff _ table '... \ 033 [0m') break set_index = pai_list.index ('set') where_index = pai_list.index ('where') set_list = re. split (R' = ',''. join (performance_list [set_index + 1: where_index]) set_field = set_list [0] set_value = set_list [1] if re. search (R' [\ '\ "] +', set_value): set_value = set_value.replace ('\'', '') set_value = set_value.replace ('\"','') where_list = re. split (R' = ',''. join (performance_list [where_index + 1:]) where_field = where_list [0] where_value = where_list [1] if re. search (R' [\ '\ "] +', where_value): where_value = where_value.replace ('\'', '') where_value = where_value.replace ('\"','') if info_list [field. get (where_field)] = where_value: info_list [field. get (set_field)] = set_value print (','. join (info_list) up_list.append (','. join (info_list) else: return up_listdef delete (staff): del_id = input ("delete id :"). strip () for I in range (len (staff): if re. split (R', ', staff [I]) [0] = del_id: staff. pop (I) print ("id: % s delete success. "% del_id) return staff else: return Falsedef main (): staff_table = 'staff_table.txt 'field = {'staff _ id': 0, 'name': 1, 'age ': 2, 'phone': 3, 'dept': 4, 'roll _ date': 5} menu = ''' \ 033 [33; 1 m -- staff_table -- \ 033 [0m \ 033 [29; 1 mS. query h. help. add q. exit E. modify D. delete \ 033 [0m''' print (menu) while True: try: with open (staff_table, 'R') as f: staff = f. readlines () choice = input ("> "). strip (). lower () if choice = 'H': # help print (menu) elif choice = 'q': break elif choice = 's': # search select (staff, field) elif choice = 'A': # add result = add (staff) if not result: print ('\ 033 [31; 1 madd failed... \ 033 [0m') continue else: with open (staff_table, 'w') as f: f. writelines (result) elif choice = 'E': # update result = update (staff, field) if not result: print ('\ 033 [31; 1 mupdate failed... \ 033 [0m') continue else: with open (staff_table, 'w') as f: for line in result: f. write (line + '\ n') elif choice = 'D': # delete result = delete (staff) if not result: print (' \ 033 [31; 1 mdelete failed... \ 033 [0m') continue else: with open (staff_table, 'w') as f: f. writelines (result) else: continue failed T: print ("\ 033 [31; 1 minput error... \ 033 [0 m ") continueif _ name _ = '_ main _': main ()
View Code

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.