I would like some help figuring out how to run individual lines from a text file through a class, this is what i have so far:
class Employee:
'Class for all employees'
empCount = 0
def __init__(self, payNo, salary, job, mName, fName, sName):
self.payNo = payNo
self.salary = salary
self.job = job
self.mName = mName
self.fName = fName
self.sName = sName
Employee.empCount += 1
def displayCount(self):
print ("Total Employee %d" % Employee.empCount)
def displayEmployee(self):
print ('{:<2s}{:<1}{:<13s}{:<15s}{:<7d}{:16s}{:>5s}{:<6d}'.format(self.sName,", ", self.mName, self.fName, self.payNo, self.job,"£",self.salary))
##print ('{:20s}{:20s}{:20s}'.format(self.sName,self.mName,self.fName))
"This would create first object of Employee class"
emp1 = Employee(12345, 2000, "Consultant", "Bartholomew", "Homer", "Simpson")
"This would create second object of Employee class"
emp2 = Employee(12346, 5000, "conultant in jobs","daniel", "matt", "li")
emp1.displayEmployee()
emp2.displayEmployee()
print ("Total Employee(s) %d" % Employee.empCount)
with open("emps.txt") as fileobject:
for line in fileobject:
print (line)
any help will be much appreciated, thank you
some may say this is more "pythonic"
[('{4}, {5} {0} {2} ${1}'.format(*emp)) for emp in
((12345, 2000, "Consultant", "Bartholomew", "Homer", "Simpson"),
(12346, 5000, "conultant in jobs","daniel", "matt", "li"))]
>>> ['Homer, Simpson 12345 Consultant $2000',
'matt, li 12346 conultant in jobs $5000']
and if this list is coming from a csv file you can:
import csv
with open('in_file.csv') as in_file:
for line in csv.reader(in_file):
print '{4}, {5} {0} {2} ${1}'.format(*line)
for currency signs, see Currency formatting in Python
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.