作业内容:
实现效果:
从info.txt文件中读取员工及其工资信息,最后将修改或增加的员工工资信息也写入原info.txt文件。
效果演示:
1. 查询员工工资
2. 修改员工工资
3. 增加新员工记录
4. 退出
思路:
进行文件读写,将读或写的数据利用strip和split方法,对文件内容进行增改查
代码:
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author: Sean_Yao """工资管理系统作业""" import sys with open("info.txt", 'r', encoding="utf-8") as f: file = list(f) msg = ''' 1. 查询员工工资 2. 修改员工工资 3. 增加新员工记录 4. 退出 ''' exit_flag = False while not exit_flag: print(msg) index_user_choice = input('>>:') if index_user_choice == '1': with open("info.txt", 'r', encoding="utf-8") as f: user_salary = f.readlines() username = input("请输入要查询的员工姓名(例如:Alex):") for user_line in user_salary: (user,salary) = user_line.strip(' ').split() if username == user: print('%s 的工资是:%s ' % (username,salary)) pass elif index_user_choice == '2': old_user = input("输入员工姓名(例如:Alex):").strip() for i in file: file = i.strip().split() if old_user in file: old_salary = file[1] new_user,new_salary = input("请输入更改后员工姓名和工资,用空格分隔(例如:Alex 10):").strip().split() with open("info.txt", "r", encoding="utf-8") as f: lines = f.readlines() with open("info.txt", "w", encoding="utf-8") as f_a: for line in lines: if old_user in line: line = line.replace(old_user,new_user) f_a.write(line) f_a.close() with open("info.txt", "r", encoding="utf-8") as f: lines = f.readlines() with open("info.txt", "w", encoding="utf-8") as f_b: for line in lines: if new_user in line: line = line.replace(old_salary,new_salary) f_b.write(line) f_b.close() print("修改成功") elif index_user_choice == '3': f = open('info.txt', 'r+', encoding='utf-8') user_salary = f.readlines() new_user_new_salary = input("请输入要增加的员工姓名和工资,共空格分割(例如:Eric 100000):") f.write(new_user_new_salary + ' ') f.close() elif index_user_choice == '4': sys.exit("再见") else: print('输入操作无效!')
info.txt文件内容:
Alex 100000 Rain 80000 Egon 50000 Yuan 30000