Python3 Exercise: To the employee information file, to achieve additions and deletions to change the operation

Source: Internet
Author: User

1. Practice Requirements:

2. Data file (data_staff.txt)

1,alex li,22,13651054684, Yun, 2013-02-042,jack wang,20,13312331232,hr,2014-06-033,mike Cao,20,15504231232,Sales, 2013-05-064,jack chen,34,12404231232,hr,2011-02-015,lu haojie,21,15204231232, Yun ' An, 2013-08-12

3. Implementing the Code

A. Main program part: Use the dictionary type to implement the function call (key corresponding value is the function name, through the name () syntax can call function execution ), take advantage of the OS module remove () and rename () Implements the modification of the file after the old file is deleted and the new file is renamed.

1 ImportOS2msg_dict={3     '1': Search,4     '2': Add,5     '3':D elete,6     '4': Change,7     '5':'Exit'8 }9 Ten  whileTrue: One     Print(""" A 1: Query - 2: Add - 3: Delete the 4: Modify - 5: Exit -    """) -Choice=input ('Enter the serial number:') +     ifChoice not inchmsg_dict: -         Print("input Error, please re-enter") +         Continue A     ifint (choice) ==5: at exit () -     Else: -Msg_dict[choice] ()

B. Query function: The transformation from string to list is implemented mainly through the split () of common operation of String, andjoin () implements the conversion of list to string.

1 defSearch ():2      #Query Mode One: SELECT * from staff_table where age >=3      #Query Mode two: SELECT * from staff_table where dept = "IT"4      #Query Method Three: SELECT * from staff_table where enroll_date like ""5 6Data=input ('Enter your query information:')7Data=data.split (' ')8Con=data[7]9Asp=data[5]TenCount=0 OneWith open ('Data_staff','R', encoding='Utf-8') as F: A         ifasp==' Age': -              forLineinchF: -               ifInt (Line.split (',') [2]) >=int (con): the                  Print(line) -Count+=1 -         elifasp=='Dept': -              forLineinchF: +                ifLine.split (',') [4]inchCon: -                    Print(line) +Count + = 1 A         Else: at              forLineinchF: -                ifLine.split (',') [5].split ('-') [0]inchCon: -                    Print(line) -Count + = 1 -         Print('end of query, a total of information found to meet the conditions%d'% count)

C. Add function: read and write using ' r+ ' read/write mode (Note that write () in this way is appended to the end of the file)

1 defAdd ():2     #add Syntax: Name,age,phone,dept,enroll-date3Data=input ('Enter the employee information you want to add:')4List_data=data.strip (). Split (',')5List_all=[]6F=open ('Data_staff','r+')7      forLineinchF:8List_all.append (Line.strip (). Split (',') [3])9     ifLIST_DATA[2]inchList_all:Ten         Print("the user already exists") One f.close () A     Else: -          forLineinchF: - F.write (line) theStaff_id=str (Len (list_all) +1) - List_data.insert (0,str (staff_id)) -F.write ('\ n') -F.write (','. Join (list_data)) + f.close () -         Print('Add Success')

D. Delete function:

1 defRemove ():2     #Delete Syntax: delete from staff_table where staff_id =3Staff_id=input ("Enter the staff_id of the employee you want to delete:")4Staff_id=staff_id.strip (). Split (' ') [6]5F=open ('Data_staff','R')6F1=open ('New_data_staff','W')7      forLineinchF:8In_list=line.split (',')9         ifin_list[0]<staff_id:Ten F1.write (line) One         elifIn_list[0]>staff_id: AIN_LIST[0]=STR (int (in_list[0))-1) -F1.write (','. Join (in_list)) -         Else: the             Continue - f.close () - f1.close () -Os.remove ('Data_staff') +Os.rename ('New_data_staff','Data_staff') -     Print('Delete succeeded')

E. Modifying a function:

1 defChange ():2     #Modify Please enter (note space and no quotation marks): UPDATE staff_table SET dept = IT WHERE dept = Operations3Data=input ("Please enter the information you want to modify:")4Old=data.split (' ') [5]5New=data.split (' ') [9]6F=open ('Data_staff','R', encoding='Utf-8')7F1=open ('New_data_staff','W', encoding="Utf-8")8      forLineinchF:9         ifOldinchLine :TenLine=line.replace (old,new) One F1.write (line) A f.close () - f1.close () -Os.remove ('Data_staff') theOs.rename ('New_data_staff','Data_staff') -     Print('Modification succeeded')

Python3 Exercise: To the employee information file, to achieve additions and deletions to change the operation

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.