0

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

  • What is the connection between the lines in the file and Employee instances? – Jon Clements Dec 06 '13 at 17:53
  • 1
    What is your question exactly? – Roman Dec 06 '13 at 17:53
  • Where do middle names go in the output scheme? – Dylan Lawrence Dec 06 '13 at 17:55
  • Welcome to stackoverflow. Your question sounds like "please write my code for me"-- at least, there is no other question visible. This type of question is not a good fit for SO. Please explain which part of the task you have difficulty with, so that people can give you helpful answers. – alexis Dec 06 '13 at 18:11
  • sorry my mistake, the main question im asking is how to run individual lines from a text file through a class, if thats possible – user3075452 Dec 06 '13 at 18:25

1 Answers1

0

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

Community
  • 1
  • 1
Guy Gavriely
  • 11,228
  • 6
  • 27
  • 42