作业内容:

实现效果:

从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 == '':
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('\n').split()
if username == user:
print('%s 的工资是:%s ' % (username,salary))
pass elif index_user_choice == '':
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 == '':
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 + '\n')
f.close()
elif index_user_choice == '':
sys.exit("再见")
else:
print('输入操作无效!')

 info.txt文件内容:

Alex 100000

Rain 80000

Egon 50000

Yuan 30000

 

最新文章

  1. 美团HD(5)-选择城市
  2. ExtJS入门教程04,这是一个超级好用的grid
  3. N年后给自己一些忠诚的建议
  4. windows下 更新 android studio SDK 到最新版本 解决方案
  5. Asp.Net运行原理(=)
  6. 安装Wamp后 Apache无法启动的解决方法
  7. 监控父元素里面子元素内容变化 DOMSubtreeModified
  8. 最锋利的Visual Studio Web开发工具扩展:Web Essentials详解
  9. .NET(C#):浅谈程序集清单资源和RESX资源
  10. Bootstrap入门(十八)组件12:徽章与巨幕
  11. 【三思笔记】 全面学习Oracle分区表及分区索引
  12. git 分支命名规范
  13. vue.js及项目实战[笔记]— 02 vue.js基础
  14. Android Studio 打包AAR和第三方静态库
  15. [转帖]以Windows服务方式运行ASP.NET Core程序
  16. 网页静态处理技术FreeMarker概述
  17. oracle第三天笔记
  18. windows环境下 nginx+iis 反向代理解决跨域问题
  19. [LeetCode系列] 双单链表共同节点搜索问题
  20. SqlServer触发器实现表的级联插入、级联更新

热门文章

  1. kafka describe 显示结果解释
  2. 在MFC中显示cmd命令行
  3. 九度-题目1026:又一版 A+B
  4. 【.Net】C# 将Access中时间段条件查询的数据添加到ListView中
  5. BZOJ 2109 航空管制(拓扑排序+贪心)
  6. noip模拟题《序》sort
  7. Selenium操作滚动条
  8. oracle job定时执行存储过程
  9. 【转】Unable to load embedded resource from assembly 无法加载的程序集嵌入的资源
  10. 【BZOJ1014】火星人(Splay,哈希)