python作業員工信息表程序(第四周)


作業需求:

1. 員工信息表程序,實現增刪改查操作:

2. 可進行模糊查詢,語法至少支持下面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"

  查到的信息,打印后,最后面還要顯示查到的條數

3. 可創建新員工紀錄,以phone做唯一鍵,staff_id需自增

4. 可刪除指定員工信息紀錄,輸入員工id,即可刪除

5. 可修改員工信息,語法如下:

  UPDATE staff_table SET dept="Market" where dept = "IT"

6. 注意:以上需求,要充分使用函數,請盡你的最大限度來減少重復代碼

 

思路解析:

1. 將原始員工信息記錄到文件

2. 讀取文件並添加到列表,進行添加刪除更新操作,並寫入到文件中

 

程序核心代碼:

peopledb

1,Jack Wang,30,43304320533,INS,2015-05-03

2,Alex Li,22,23651054608,IT,2013-04-01

員工信息表.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: Colin Yao
"""python 員工信息表操作"""
import sys
import os


def select1():
    '''
    查看文件函數
    :return: 
    '''
    with open('peopledb', 'r', encoding='utf-8') as f:
        line = f.readlines()
        for i in line:
            print(i)
def select():
    '''
    查詢函數
    :return: 
    '''
    msg = '''
    請輸入或復制查詢命令例如:

        1. select name,age from staff_table where age > 22
        2. select * from staff_table where dept = "IT"
          3. select * from staff_table where enroll_date like "2013"
    '''
    print(msg)
    user_choice_input = input(">>>:")
    user_choice_input1 = user_choice_input.split(' ')
    if user_choice_input == 'select name,age from staff_table where age > %s' % (user_choice_input1[7]):
        with open('peopledb', 'r+', encoding='utf-8') as f:
            list1 = []
            count = 0
            for line in f:
                i = line.strip().split(',')
                if i[2] > user_choice_input1[7]:
                    list1.append(i)
            for s in list1:
                count = count + 1
            for j in list1:
                print(j)
            print('滿足條件的個數為>>:%s' % (count))
    elif user_choice_input == ('select * from staff_table where dept = %s' % (user_choice_input1[7])):
        with open('peopledb', 'r+', encoding='utf-8') as f:
            list2 = []
            count = 0
            for line in f:
                i1 = line.strip().split(',')
                if i1[4] == eval(user_choice_input1[7]):
                    list2.append(i1)
            for s1 in list2:
                count = count + 1
                # print(list1)
            for j1 in list2:
                print(j1)
            print('滿足條件的個數為>>:%s' % (count))
    elif user_choice_input == ('select * from staff_table where enroll_date like %s' % (user_choice_input1[7])):
        with open('peopledb', 'r+', encoding='utf-8') as f:
            list3 = []
            list4 = []
            count = 0
            for line in f:
                i = line.strip().split(',')
                list3.append(i)
            for j in list3:
                m = j[5].split('-')
                if m[0] == eval(user_choice_input1[7]):
                    list4.append(j)
            for s in list4:
                count = count + 1
                if count < 1:
                    print("沒有找到類似的條目:")
                    pass
                else:
                    pass
            for j in list4:
                print(j)
            print('滿足條件的條數為>>:%s' % (count))
    return ()


def alter():
    '''
    添加函數
    :return: 
    '''
    msg = '''

    1)添加命令如下:Jack Wang,30,13304320533,HR,2015-05-03

    '''
    print(msg)
    user_choice_input = input("請輸入命令>>>:")
    user_choice_input1 = user_choice_input.split(',')
    with open('peopledb', 'r+', encoding='utf-8') as f:
        list = []
        for line in f:
            s2 = line.strip().split(',')
            m = s2[3]
            list.append(m)
        if user_choice_input1[2] in list:
            print('這條記錄已經存在')
            main()
        else:
            my_index = str(len(list) + 1)
            user_choice_input1.insert(0, my_index)
            user_choice_input1 = ','.join(user_choice_input1)
        f.write('\n')
        f.write(user_choice_input1)
        f.close()
        print("記錄添加完成", '\n')
        select1()
    return ()


def delect():
    '''
    刪除函數
    :return: 
    '''
    print("請輸入刪除命令例如: 輸入用戶ID 既可以從staff_table里刪除")
    msg = '''
        1)按1 刪除、直接刪除ID即可
        2)按2或者q退出
        3)按任意返回上一層
    '''
    print(msg)
    user_choice_input = input("請輸入命令>>>: ")
    if user_choice_input == '1':
        print("現有的用戶為:")
        select1()
        print('\n')
        user_choice_input1 = input("請輸入需要刪除的用戶ID:")
        user_choice_input2 = user_choice_input1[0]
        f = open('peopledb', 'r+', encoding='utf-8')
        f1 = open('new_peopledb', 'w+', encoding='utf-8')
        for line in f:
            i = line.strip().split(',')
            i1 = i[0]
            if user_choice_input2 != i1:
                i = ','.join(i)
                f1.write(i)
                f1.write('\n')
            else:
                continue
        f.close()
        f1.close()
        os.remove('peopledb')
        os.rename('new_peopledb', 'peopledb')
        print('\n')
        select1()
    elif user_choice_input == '2' or 'q':
        sys.exit()
    return ()


def update():
    '''
    更新函數
    :return: 
    '''
    msg = '''
    1)這里第一個等號按照沒有空格的格式划分
    2)命令范例:UPDATE staff_table SET dept="INS" where dept = "HR"
    '''
    print(msg)
    user_choice_input = input("請輸入命令>>>:")
    user_choice_input1 = user_choice_input.split(' ')
    dept = user_choice_input1[3].split('=')
    dept_new = dept[1]
    dept_old = user_choice_input1[7]
    if user_choice_input == ('UPDATE staff_table SET dept=%s where dept = %s' % (dept_new, dept_old)):
        dept_new1 = eval(dept_new)
        dept_old1 = eval(dept_old)
        f = open('peopledb', 'r+', encoding='utf-8')
        f1 = open('new_peopledb', 'w+', encoding='utf-8')
        for line in f:
            i = line.strip().split(',')
            dept_change = i[4]
            if dept_change == dept_old1:
                i[4] = eval(dept_new)
            i = ','.join(i)
            f1.write(i)
            f1.write('\n')
        f.close()
        f1.close()
        os.remove('peopledb')
        os.rename('new_peopledb', 'peopledb')
        print('\n')
        select1()
        pass
    return ()


def main():
    '''
    交互程序
    :return: 
    '''
    print("員工信息表操作作業練習")
    msg = '''
        1)查詢
        2)添加
        3)刪除
        4)更新
        5) 退出
      '''
    exit_flag = False
    while not exit_flag:
        print(msg)
        user_choice = input("請選擇>>:")
        if user_choice == '1':
            select()
        elif user_choice == '2':
            alter()
        elif user_choice == '3':
            delect()
        elif user_choice == '4':
            update()
        elif user_choice == '5' or 'q':
            sys.exit()
        else:
            print("您的選擇有誤、請重新輸入")


main()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM