需求如下:
工资管理系统aaa 100000bbb 80000ccc 50000ddd 30000-----以上是info.txt文件-----实现效果:从info.txt文件中读取员工及其工资信息,最后将修改或增加的员工工资信息也写入原info.txt文件。效果演示:1. 查询员工工资2. 修改员工工资3. 增加新员工记录4. 退出
1 #!/usr/bin/env python 2 #-*- coding:utf-8 -*- 3 # @Time : 2017/9/21 15:19 4 # @Author : lichuan 5 # @File : salary.py 6 7 import os 8 25 26 def select(read,write): 27 i=input("please input the employee name:").strip() 28 for line in read: 29 if i == line.split()[0]: 30 print("%s的工资是:%s" % (line.split()[0],line.split()[1])) 31 return 32 print("输入错误,请重试!") 33 read.seek(0) 34 select(read, write) 35 36 def modify(read,write): 37 # import os 38 FLAG_TAG=True 39 m=input("请输入要修改的员工姓名和工资,以空格分隔").strip().split() 40 if len(m) !=2: 41 print("输入错误,请重试,以空格分隔!") 42 modify(read,write) 43 return 44 for i in read: 45 if i.split()[0] == m[0] and m[1].isdigit(): 46 FLAG_TAG=False 47 i = "%s %s\n" % (m[0], m[1]) 48 write.write(i) 49 if FLAG_TAG: 50 print("输入错误,请重试!") 51 read.seek(0) 52 write.seek(0) 53 modify(read,write) 54 else: 55 read.close() 56 write.close() 57 os.remove("info.txt") 58 os.rename(".swap", "info.txt") 59 60 61 62 def add(read,write): 63 # import os 64 a=input("请输入员工姓名和工资,以空格分隔").strip().split() 65 if len(a) !=2: 66 print("输入错误,请重试,以空格分隔!") 67 add(read,write) 68 return 69 70 for i in read: 71 if i.split()[0] == a[0]: 72 print("用户已存在,请重新添加新用户!") 73 read.seek(0) 74 write.seek(0) 75 add(read,write) 76 return 77 write.write(i) 78 write.write("\n%s %s" %(a[0],a[1])) 79 read.close() 80 write.close() 81 os.remove("info.txt") 82 os.rename(".swap","info.txt") 83 84 85 86 def out(read,write): 87 88 print("再见!") 89 # FLAG_TAG=False 90 91 item={ 92 1:"查询员工工资", 93 2:"修改员工工资", 94 3:"增加新员工记录", 95 4:"退出" 96 } 97 98 fun={ 99 1:select, 100 2:modify, 101 3:add, 102 4:out 103 } 104 105 FLAG_TAG=True 106 107 while FLAG_TAG: 108 for i in range(1,5): 109 print("%d, %s" %(i,item[i])) 110 choice=input("请选择:").strip() 111 with open(‘info.txt‘,‘r‘,encoding="utf-8") as r_read,open(‘.swap‘,‘w‘,encoding="utf-8") as r_write: 112 if choice.isdigit() and int(choice) >=1 and int(choice)<4: 113 choice=int(choice) 114 fun[choice](r_read,r_write) 115 continue 116 elif choice.isdigit() and int(choice) == 4: 117 FLAG_TAG=False 118 fun[int(choice)](r_read,r_write) 119 break 120 else: 121 print("wrong input,try again!") 122 continue 123 FLAG_TAG=False
时间: 2024-10-03 02:45:04