要实现功能完整的个人员管理程序,可以按以下步骤进行:
1. 确定需求和数据结构
首先需要确定个人员管理程序的需求,例如需要储存和管理的信息类型,比如姓名、年龄、性别等。在此基础上,可以选择合适的数据结构来储存和处理信息。比如可以使用Python中的字典(dict)或列表(list)。
2. 实现基本的增删改查功能
根据需求和数据结构,可以实现基本的增删改查功能。比如可以用字典来存储每个人的信息,并实现增加、删除、修改和查询个人信息的功能代码,如下所示:
people = {} # 存储人员信息的字典
# 添加一个人员信息
def add_person(name, age, sex):
people[name] = {"age": age, "sex": sex}
# 删除一个人员信息
def del_person(name):
if name in people:
del people[name]
# 修改一个人员信息
def modify_person(name, age=None, sex=None):
if name in people:
if age:
people[name]["age"] = age
if sex:
people[name]["sex"] = sex
# 查询一个人员信息
def query_person(name):
if name in people:
return people[name]
else:
return None
3. 实现文件读写操作
为保存和读取数据,可以使用Python中的文件读写操作。使用文件系统来存储数据有一个优点是即使在程序关闭的情况下,数据仍然可以保存在文件中,并在程序再次启动时读取。下面是一个示例,可以将数据保存在CSV文件中:
import csv
# 保存人员信息到CSV文件中
def save_people_to_csv(file_path):
with open(file_path, mode='w', newline='') as file:
fieldnames = ['name', 'age', 'sex']
writer = csv.DictWriter(file, fieldnames=fieldnames)
writer.writeheader()
for name, info in people.items():
writer.writerow({'name': name, 'age': info['age'], 'sex': info['sex']})
# 从CSV文件中读取人员信息
def load_people_from_csv(file_path):
try:
with open(file_path) as file:
reader = csv.DictReader(file)
for row in reader:
add_person(row['name'], int(row['age']), row['sex'])
except FileNotFoundError:
print("The file is not exist!")
示例1:完整代码实现
import csv
people = {} # 存储人员信息的字典
# 添加一个人员信息
def add_person(name, age, sex):
people[name] = {"age": age, "sex": sex}
# 删除一个人员信息
def del_person(name):
if name in people:
del people[name]
# 修改一个人员信息
def modify_person(name, age=None, sex=None):
if name in people:
if age:
people[name]["age"] = age
if sex:
people[name]["sex"] = sex
# 查询一个人员信息
def query_person(name):
if name in people:
return people[name]
else:
return None
# 保存人员信息到CSV文件中
def save_people_to_csv(file_path):
with open(file_path, mode='w', newline='') as file:
fieldnames = ['name', 'age', 'sex']
writer = csv.DictWriter(file, fieldnames=fieldnames)
writer.writeheader()
for name, info in people.items():
writer.writerow({'name': name, 'age': info['age'], 'sex': info['sex']})
# 从CSV文件中读取人员信息
def load_people_from_csv(file_path):
try:
with open(file_path) as file:
reader = csv.DictReader(file)
for row in reader:
add_person(row['name'], int(row['age']), row['sex'])
except FileNotFoundError:
print("The file is not exist!")
# 测试
if __name__ == '__main__':
load_people_from_csv("people.csv") # 从CSV文件中读取人员信息
add_person("张三", 30, "男") # 添加人员信息
save_people_to_csv("people.csv") # 保存人员信息到CSV文件中
del_person("张三") # 删除人员信息
modify_person("李四", age=35) # 修改人员信息
query_person("李四") # 查询人员信息
示例2:使用面向对象方式实现
import csv
class People:
def __init__(self, filepath):
self.filepath = filepath
self.people = {}
self.load()
def add(self, name, age, sex):
self.people[name] = {"age": age, "sex": sex}
self.save()
def delete(self, name):
if name in self.people:
del self.people[name]
self.save()
def modify(self, name, age=None, sex=None):
if name in self.people:
if age:
self.people[name]["age"] = age
if sex:
self.people[name]["sex"] = sex
self.save()
def query(self, name):
if name in self.people:
return self.people[name]
else:
return None
def save(self):
with open(self.filepath, mode='w', newline='') as file:
fieldnames = ['name', 'age', 'sex']
writer = csv.DictWriter(file, fieldnames=fieldnames)
writer.writeheader()
for name, info in self.people.items():
writer.writerow({'name': name, 'age': info['age'], 'sex': info['sex']})
def load(self):
try:
with open(self.filepath) as file:
reader = csv.DictReader(file)
for row in reader:
self.add(row['name'], int(row['age']), row['sex'])
except FileNotFoundError:
print("The file is not exist!")
# 测试
if __name__ == '__main__':
people = People("people.csv")
people.add("张三", 30, "男") # 添加人员信息
people.delete("张三") # 删除人员信息
people.modify("李四", age=35) # 修改人员信息
people.query("李四") # 查询人员信息
以上就是实现功能完整的个人员管理程序的攻略,通过使用Python中的字典、列表、文件读写操作、面向对象等特性,可以使程序设计更加简洁、易于维护和拓展。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Python实现功能完整的个人员管理程序 - Python技术站