一个python实现的employee类,python实现employee,'''Class_Str
一个python实现的employee类,python实现employee,'''Class_Str
'''Class_StructEmp2_file.pymimic a C Structure or Pascal Record using a classload the data from a csv type file like this:John Q. Johnson,computer research,3500Loyd Tetris,Human Resources,6000Mark Marksman,external development,4800tested with Python27 and Python32 by vegaseat'''class Employee(): """ mimics a C Structure or Pascal Record """ def __init__(self, name, dept, salary): self.name = name self.dept = dept self.salary = salarydef by_last_name(record): """ helper function to sort by last name assume names are "first middle last" """ return record.name.split()[-1]def by_department(record): """ helper function to sort by department case-insensitive """ return record.dept.lower()# load the data filefname = "employee_data.txt"with open(fname, "r") as fin: employee_data = fin.readlines()record_list = []for line in employee_data: line = line.rstrip() #print(line, type(line), line.split(',')) # test name, dept, salary = line.split(',') #print(name, dept, salary) # test record_list.append(Employee(name, dept, salary))print("Employee names:")# explore the record_list by instancefor emp in record_list: print(emp.name)print('-'*35)print("Employee names sorted by last name:")# sort the records by last name using a helper functionfor emp in sorted(record_list, key=by_last_name): print(emp.name)print('-'*35)print("High paid employees:")# list the folks that get more than $4000 salary per monthfor emp in record_list: if float(emp.salary) > 4000: print("%s gets more then $4000 a month" % emp.name)print('-'*35)print("Sorted by department:")# sort the records by case insensitive department namefor emp in sorted(record_list, key=by_department): print("%s works in %s" % (emp.name, emp.dept))
'''result --;>Employee names;:John Q.; Johnson;Loyd Tetris;Mark Marksman;
Employee names; sorted; by; last; name;:John Q.; Johnson;Mark Marksman;Loyd Tetris;
High paid; employees;:Loyd Tetris; gets; more; then; $4000 a; month;Mark Marksman; gets; more; then; $4000 a; month;
Sorted by; department;:John Q.; Johnson; works; in; computer; research;Mark Marksman; works; in; external; development;Loyd Tetris; works; in; Human; Resources;'''
相关内容
- Python统计文件夹下所有文件名与内容,,#!/usr/bin/e
- python从任意文件读取邮件地址输出,,# Hello, thi
- python搜索指定目录的文件,并进行文件内搜索指定的关
- python获取一组汉字拼音的首字母,python获取一组汉字
- python各种删除空格的方法汇总,python空格汇总," xy
- python更新列表(数组),python更新列表数组,aList = [12
- python模拟实现线程池,python线程池,# !/usr/bin/
- Python读取文件,python读取, f = open("c
- python按字符翻转字符串,python翻转字符串,New in; pyth
- python清除字符串里的非字母字符,python字符串字符,s
评论关闭