Realization of employee information management system with Python
- 2021-09-05 00:21:14
- OfStack
Want to realize a staff management system
First, let's look at what functions we want to implement in 1
The most basic additions, deletions and changes must be realized
Then add 1 data display, data sorting and data statistics functions
The code is directly on the following
Add employee data
# Receive user revenue
id = input(' Please enter the employee number ')
name = input(' Please enter a name ')
sex = input(' Please enter gender ')
age = input(' Please enter an age ')
education = input(' Please enter educational background ')
address = input(' Please enter your address ')
photonumber = input(' Please enter the phone number ')
money = input(' Please enter salary ')
# To add data to a list
data.append([id, name, sex, age, education, address, photonumber, money])
print(' Successful addition ')
# Call the save function Save data
save()
Delete employee data
id = input(' Please enter the employee number you want to modify ')
ids = [i[0] for i in data]
if id not in ids:
print(' The employee you inquired about does not exist ')
return
else:
del data[ids.index(id)]
print(' Delete succeeded ')
save()
Query employee data
# Select a query target
flag = int(input('1. Query by employee number 2. Inquire according to the name of the employee '))
if flag == 1:
id = input(' Enter employee number ')
# Employee number list
ids = [i[0] for i in data]
# Determining whether the input number exists or not
if id not in ids:
print(' The employee you inquired about does not exist ')
return
else:
print(' Employee number Name Gender Age Educational background Address Telephone Salary ')
# Print information for this number
for i in data[ids.index(id)]:
print(i, end=' ')
print()
else:
name = input(' Enter employee name ')
# List of Employee Names
names = [i[1] for i in data]
# Determine whether the entered name exists
if name not in names:
print(' The employee you inquired about does not exist ')
return
else:
print(' Employee number Name Gender Age Educational background Address Telephone Salary ')
# Ibid.
for i in data[names.index(name)]:
print(i, end=' ')
print()
Modify employee information
id = input(' Please enter the employee number you want to modify ')
ids = [i[0] for i in data]
if id not in ids:
print(' The employee you inquired about does not exist ')
return
else:
# Enter the data to be modified
name = input(' Please enter a name ')
sex = input(' Please enter gender ')
age = input(' Please enter an age ')
education = input(' Please enter educational background ')
address = input(' Please enter your address ')
photonumber = input(' Please enter the phone number ')
money = input(' Please enter salary ')
# Modify data
data[ids.index(id)] = [id, name, sex, age, education, address, photonumber, money]
print(' Successful modification ')
save()
Sorting function
global data
data = sorted(data, key=lambda x: x[1])
Statistical function
counts = {}
# Count the number of people per salary
for i in data:
counts[int(i[-1])] = counts.get(i[-1], 0) + 1
# Sort by number of people
counts = dict(sorted(counts.items(), key=lambda x: x[1], reverse=True))
# Print the results
for money, count in counts.items():
print('{0:<10}{1:>5}'.format(money, count))
print(' The highest wages are: ', max(counts))
print(' The lowest wages are: ', min(counts))
Display function
# Print title
print(' Employee number Name Gender Age Educational background Address Telephone Salary ')
# Traversing the data list Then print the data
for i in data:
for j in i:
print(j, end=' ')
print()
Read and save function
def save(): # Save function
# Open a file and write data
with open(' Data .csv','w') as j:
for i in data:
j.write(','.join(i)+'\n')
j.close()
def load(): # Read function
# Read a file
with open(' Data .csv','r') as j:
# Read each row of data
for i in j.readlines():
# Clean out line breaks Then divide it with comma as spacer
data.append(i.replace('\n','').split(','))
j.close()
Summarize the overall code:
def add(): # 添加数据函数
# 接收用户收入
id = input('请输入职工号')
name = input('请输入姓名')
sex = input('请输入性别')
age = input('请输入年龄')
education = input('请输入学历')
address = input('请输入住址')
photonumber = input('请输入电话')
money = input('请输入工资')
# 向列表中添加数据
data.append([id, name, sex, age, education, address, photonumber, money])
print('添加成功')
# 调用保存函数 保存数据
save()
def show(): # 显示函数
# 打印标题
print('职工号 姓名 性别 年龄 学历 住址 电话 工资')
# 遍历数据列表 然后打印数据
for i in data:
for j in i:
print(j, end=' ')
print()
def quety(): # 查询函数
# 选择查询目标
flag = int(input('1.按照职工编号查询 2.按照职工姓名查询'))
if flag == 1:
id = input('输入职工编号')
# 职工编号列表
ids = [i[0] for i in data]
# 判断输入的编号是否存在
if id not in ids:
print('您查询的职工不存在')
return
else:
print('职工号 姓名 性别 年龄 学历 住址 电话 工资')
# 打印该编号的信息
for i in data[ids.index(id)]:
print(i, end=' ')
print()
else:
name = input('输入职工姓名')
# 职工姓名列表
names = [i[1] for i in data]
# 判断输入的姓名是否存在
if name not in names:
print('您查询的职工不存在')
return
else:
print('职工号 姓名 性别 年龄 学历 住址 电话 工资')
# 同上
for i in data[names.index(name)]:
print(i, end=' ')
print()
def modify(): # 修改函数
# 原理同上
id = input('请输入你要修改的职工编号')
ids = [i[0] for i in data]
if id not in ids:
print('您查询的职工不存在')
return
else:
# 输入要修改的数据
name = input('请输入姓名')
sex = input('请输入性别')
age = input('请输入年龄')
education = input('请输入学历')
address = input('请输入住址')
photonumber = input('请输入电话')
money = input('请输入工资')
# 修改数据
data[ids.index(id)] = [id, name, sex, age, education, address, photonumber, money]
print('修改成功')
save()
def sort(): # 排序函数
global data
data = sorted(data, key=lambda x: x[1])
def statistics(): # 统计函数
counts = {}
# 统计每个工资的人数
for i in data:
counts[int(i[-1])] = counts.get(i[-1], 0) + 1
# 按照人数多少排序
counts = dict(sorted(counts.items(), key=lambda x: x[1], reverse=True))
# 将结果打印
for money, count in counts.items():
print('{0:<10}{1:>5}'.format(money, count))
print('工资最多的是:', max(counts))
print('工资最少的是:', min(counts))
def delete(): # 删除函数
# 原理同上
id = input('请输入你要修改的职工编号')
ids = [i[0] for i in data]
if id not in ids:
print('您查询的职工不存在')
return
else:
del data[ids.index(id)]
print('删除成功')
save()
def save(): # 保存函数
# 打开文件,写入数据
with open('数据.csv','w') as j:
for i in data:
j.write(','.join(i)+'\n')
j.close()
def load(): # 读取函数
# 读取文件
with open('数据.csv','r') as j:
# 读取每行数据
for i in j.readlines():
# 清洗掉换行符 然后以逗号为间隔符分割
data.append(i.replace('\n','').split(','))
j.close()
if __name__ == '__main__':
data = [] # 数据保存列表
# 读取文件 如果文件不存在 报错跳过 无视
try:
load()
except FileNotFoundError:
pass
while True:
# 根据玩家的输入 选择相应的功能
choice = int(input('1.添加职工数据\n2.显示职工数据\n3.查询职工数据\n4.修改职工数据\n5.删除职工数据\n6.保存职工数据\n7.排序职工数据\n8.统计职工工资数据\n9.退出'))
if choice == 1:
add()
elif choice == 2:
show()
elif choice == 3:
quety()
elif choice == 4:
modify()
elif choice == 5:
delete()
elif choice == 6:
save()
elif choice == 7:
sort()
elif choice == 8:
statistics()
elif choice == 9:
print('退出程序')
break