Python3练习:对员工信息文件,实现增删改查操作

1.练习要求:

2.数据文件(data_staff.txt)

1,Alex Li,22,13651054684,运维,2013-02-04
2,Jack Wang,20,13312331232,HR,2014-06-03
3,Mike Cao,20,15504231232,Sales,2013-05-06
4,Jack Chen,34,12404231232,HR,2011-02-01
5,Lu Haojie,21,15204231232,运维,2013-08-12

 

3.实现代码

A.主程序部分:采用字典类型来实现函数的调用(key对应的value是函数名,通过Name()语法可以调用函数执行),利用os模块的remove()和rename()实现文件的修改后的旧文件删除和新文件的重命名。

 1 import os
 2 msg_dict={
 3     '1':Search,
 4     '2':Add,
 5     '3':Delete,
 6     '4':Change,
 7     '5':'退出'
 8 }
 9 
10 while True:
11     print( """
12    1:查询
13    2:添加
14    3:删除
15    4:修改
16    5:退出
17    """)
18     choice=input('输入序号:')
19     if choice not in msg_dict:
20         print("输入错误,请重新输入")
21         continue
22     if int(choice)==5:
23         exit()
24     else:
25         msg_dict[choice]()

 

B.查询函数:主要通过字符串常用操作的split()实现从字符串到列表的转换,join()实现列表到字符串的转换

 1 def Search():
 2      # 查询方式一:select * from staff_table where age >= 22
 3      # 查询方式二:select * from staff_table where dept = "IT"
 4      # 查询方式三:select * from staff_table where enroll_date like "2013"
 5 
 6      data=input('输入您的查询信息:')
 7      data=data.split(' ')
 8      con=data[7]
 9      asp=data[5]
10      count=0
11      with open('data_staff','r',encoding='utf-8') as f:
12         if asp=='age':
13             for line in f:
14               if int(line.split(',')[2])>=int(con):
15                  print(line)
16                  count+=1
17         elif asp=='dept':
18             for line in f:
19                if line.split(',')[4] in con:
20                    print(line)
21                    count += 1
22         else:
23             for line in  f:
24                if line.split(',')[5].split('-')[0] in con:
25                    print(line)
26                    count += 1
27         print('查询结束,共查到符合条件的信息 %d 条 ' % count)

C.添加函数:利用‘r+’的可读写模式来实现读写(注意这种方式下的write()是追加到文件末尾的)

 1 def Add():
 2     # 添加语法: name,age,phone,dept,enroll-date
 3     data=input('输入您要添加的员工信息:')
 4     list_data=data.strip().split(',')
 5     list_all=[]
 6     f=open('data_staff','r+')
 7     for line in f:
 8         list_all.append(line.strip().split(',')[3])
 9     if list_data[2] in list_all:
10         print("该用户已经存在")
11         f.close()
12     else:
13         for line in f:
14             f.write(line)
15         staff_id=str(len(list_all)+1)
16         list_data.insert(0,str(staff_id))
17         f.write('\n')
18         f.write(','.join(list_data))
19         f.close()
20         print('添加成功')

 

D.删除函数:

 1 def Remove():
 2     # 删除语法:delete from staff_table where staff_id = 12
 3     staff_id=input("输入您要删除员工的Staff_id:")
 4     staff_id=staff_id.strip().split(' ')[6]
 5     f=open('data_staff','r')
 6     f1=open('new_data_staff','w')
 7     for line in f:
 8         in_list=line.split(',')
 9         if in_list[0]<staff_id:
10             f1.write(line)
11         elif in_list[0]>staff_id:
12             in_list[0]=str(int(in_list[0])-1)
13             f1.write(','.join(in_list))
14         else:
15             continue
16     f.close()
17     f1.close()
18     os.remove('data_staff')
19     os.rename('new_data_staff','data_staff')
20     print('删除成功')

E.修改函数:

 1 def Change():
 2     # 修改请输入(注意空格和没有引号):UPDATE staff_table SET dept = IT where dept = 运维
 3     data=input("请输入您要修改的信息:")
 4     old=data.split(' ')[5]
 5     new=data.split(' ')[9]
 6     f=open('data_staff','r',encoding='utf-8')
 7     f1=open('new_data_staff','w',encoding="utf-8")
 8     for line in f:
 9         if old in line:
10              line=line.replace(old,new)
11         f1.write(line)
12     f.close()
13     f1.close()
14     os.remove('data_staff')
15     os.rename('new_data_staff','data_staff')
16     print('修改成功')

 

转载于:https://www.cnblogs.com/luhaojie/p/7296587.html

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值