Reads each line from the file and supply it to the employee function

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 880 Views - Last Post: 03 December 2012 - 07:39 AM Rate Topic: -----

#1 TeaAnyOne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 02-December 12

Reads each line from the file and supply it to the employee function

Posted 02 December 2012 - 08:08 AM

12347 10000 secretary James Harrison
12348 20000 secretary Jade Powell
12341 40000 consultant Adam Johnson

Hi, lets just say that the text above is a text file. I would like to open the file whose name is supplied, reads each line from the file and supplies it to the employee-creation function, storing the results returned by this function in a list.

Im getting errors such as:

Traceback (most recent call last):
  File "C:\Users\Ken\Desktop\Uni Work\Programming\Assignment 2\ex1.py", line 22, in <module>
    newEmployee(myLines)
  File "C:\Users\Ken\Desktop\Uni Work\Programming\Assignment 2\ex1.py", line 9, in newEmployee
    list = myLines.split(" ")
AttributeError: 'list' object has no attribute 'split'


Below is the code that ive got, any help?

f = open("A text file", "r");
print(f.read())
print(f.readline())
myLines = f.readlines()
for line in myLines:
    print(line)

def newEmployee(s):
     list = myLines.split(" ")
     number = int(list[0])
     salary = int(list[1])
     jobTitle = list[2]
     name = list[3]
     surname = list[4]
     print(number, salary, jobTitle, name, surname)

def __str__(self):

    return format(self.payroll, "d") + format(self.salary, "d") + ' ' \
          + self.jobtitle + self.name  + self.surname

newEmployee(myLines)

class Employee:
      def __init__(self, number, salary, jobTitle, name, surname):
          self.number = number
          self.salary = salary
          self.jobTitle = jobTitle
          self.name = name
          self.surname = surname


Is This A Good Question/Topic? 0
  • +

Replies To: Reads each line from the file and supply it to the employee function

#2 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Reads each line from the file and supply it to the employee function

Posted 02 December 2012 - 08:35 AM

You shouldn't call a variable "list", it's a Python name for list objects, but unfortunately in your case, it would let you reassign it without warning.

Change "list" to "lst" or another variable name and try again :)
Was This Post Helpful? 0
  • +
  • -

#3 Tayacan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • Posts: 275
  • Joined: 18-January 11

Re: Reads each line from the file and supply it to the employee function

Posted 02 December 2012 - 08:58 AM

I don't think that's the cause of your error, though Simown is of course right.

What is myLines? It's the list of all the lines in the file. I think what you want is to use split() on just one line.
Was This Post Helpful? 0
  • +
  • -

#4 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Reads each line from the file and supply it to the employee function

Posted 02 December 2012 - 09:13 AM

That's the error given by the trace. An attribute error caused by reassigning the list object.

It is the cause of that particular error, but then again, I don't know the desired behaviour of the rest of the program.

I agree with Tayacan, you want to do something like:
for line in myLines:
    l = myLines.split(" ")
    ...
    # Data processing code here


Was This Post Helpful? 0
  • +
  • -

#5 Tayacan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • Posts: 275
  • Joined: 18-January 11

Re: Reads each line from the file and supply it to the employee function

Posted 02 December 2012 - 09:33 AM

Isn't the attribute error because he tries to call split on a list rather than on a string?
>>> [1,2,3].split(" ")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'split'
>>> list = [1,2,3]
>>> list
[1, 2, 3]

Was This Post Helpful? 1
  • +
  • -

#6 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Reads each line from the file and supply it to the employee function

Posted 02 December 2012 - 10:04 AM

Ah, yeah, my mistake!

I was thinking it was a single line but it's still a list of lines from reading. Either way it's bad ;)
Was This Post Helpful? 0
  • +
  • -

#7 TeaAnyOne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 02-December 12

Re: Reads each line from the file and supply it to the employee function

Posted 02 December 2012 - 02:03 PM

Ok, ive changed my code a little. I made the text file into a list and need the Employee class to recognize the list and store all the data within it.

class Employee(object):
      def __init__(self, number, salary, jobTitle, name, surname):
          self.number = number
          self.salary = salary
          self.jobTitle = jobTitle
          self.name = name
          self.surname = surname
          print(number, salary, jobTitle, name, surname)


def __str__(self):

    return format(self.payroll, "d") + format(self.salary, "d") + ' ' \
          + self.jobtitle + self.name  + self.surname

f = open('a text file', 'r')
Employeelist = [line.split(' ') for line in f.readlines()]

Employee(Employeelist)


and getting the error:

Traceback (most recent call last):
  File "C:\Users\Ken\Desktop\Uni Work\Programming\Assignment 2\ex1.py", line 20, in <module>
    Employee(Employeelist)
TypeError: __init__() takes exactly 6 positional arguments (2 given)


Im really stuck :S Any more Ideas?
Was This Post Helpful? 0
  • +
  • -

#8 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Reads each line from the file and supply it to the employee function

Posted 02 December 2012 - 02:12 PM

You are passing it 2 parameters "self" and "Employeelist", you need to give it 5 parameters + self.

The easiest way would be to unpack the list in the parameter. Assuming Employeelist is a list of 5 elements:
Employee(*Employeelist)

Was This Post Helpful? 1
  • +
  • -

#9 Tayacan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • Posts: 275
  • Joined: 18-January 11

Re: Reads each line from the file and supply it to the employee function

Posted 02 December 2012 - 02:13 PM

Alright, so the problem here is that when you do this:
Employee(EmployeeList)


You're actually calling the __init__ method of your Employee class. Now, if you look at that method, it takes six arguments: self, number, salary, jobTitle, name and surname. However, you're only giving it one argument, which happens to be a list of lists.

Okay, so the first thing we need to clear up is this: Do you want each Employee object to store one employee, or do you want one big object that stores all of them? In the first case (which would be the nicest, I think) you need to change your __init__ method so it takes just two arguments - self and a list. That list would contain the data for exactly one employee, or one line of your text file, split up by the split() function.

Then you can make the list of all your employees with a list comprehension:
EmployeeList = [Employee(line.split()) for line in f.readlines()]

Was This Post Helpful? 0
  • +
  • -

#10 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Reads each line from the file and supply it to the employee function

Posted 02 December 2012 - 02:19 PM

@Tayacan the Employee constructor still won't take a list, even a single level one.

Combine Tayacan's solution to get every Employee in the file (assuming it's a list of more than 1 employee) and then unpack the variables as parameters to the function as I suggested.

This post has been edited by Simown: 02 December 2012 - 02:19 PM

Was This Post Helpful? 0
  • +
  • -

#11 Tayacan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • Posts: 275
  • Joined: 18-January 11

Re: Reads each line from the file and supply it to the employee function

Posted 02 December 2012 - 02:30 PM

@Simown, that why I'm saying he should change his constructor so that it will. I imagined that he could unpack his list inside the constructor.
class Employee(object):
      def __init__(self, lst):
          self.number = lst[0]
          self.salary = lst[1]
          self.jobTitle = lst[2]
          self.name = lst[3]
          self.surname = lst[4]


It's not so pretty, but I thought it best not to introduce new syntax - make the code work, then make it pretty.
Was This Post Helpful? 1
  • +
  • -

#12 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Reads each line from the file and supply it to the employee function

Posted 02 December 2012 - 02:33 PM

True, true. I misunderstood what you meant, apologies! :)
Was This Post Helpful? 0
  • +
  • -

#13 TeaAnyOne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 02-December 12

Re: Reads each line from the file and supply it to the employee function

Posted 02 December 2012 - 02:51 PM

Ok guys we are making some progress :) Thanks to you :P and ive got one last question.

my new code:

class Employee(object):
    def __init__(self, lst):
	    self.number = lst[0]
	    self.salary = lst[1]
	    self.jobTitle = lst[2]
	    self.name = lst[3]
	    self.surname = lst[4]
	    print(lst)

def __str__(self):

    return format(self.payroll, "d") + format(self.salary, "d") + ' ' \
          + self.jobTitle + self.name  + self.surname

f = open('a text file', 'r')
lst = [Employee(line.split()) for line in f.readlines()]

Employee(lst)


but when I run the program, It prints out all the employess like it should but then it prints out this:

['13129', '25000', 'technician', 'River', 'Storm']
['13130', '22000', 'secretary', 'Rose', 'Tyler']
[<__main__.Employee object at 0x02A41F90>, <__main__.Employee object at 0x02A41FB0>, <__main__.Employee object at 0x02A41FD0>, <__main__.Employee object at 0x02A48370>, <__main__.Employee object at 0x02A483D0>, <__main__.Employee object at 0x02A48430>, <__main__.Employee object at 0x02A48470>, <__main__.Employee object at 0x02A48510>, <__main__.Employee object at 0x02A486D0>, <__main__.Employee object at 0x02A487F0>, <__main__.Employee object at 0x02A488B0>, <__main__.Employee object at 0x02A48970>, <__main__.Employee object at 0x02A489B0>, <__main__.Employee object at 0x02A48AD0>]


its probs a simple fix, but im not sure why its printing it out :P
Was This Post Helpful? 0
  • +
  • -

#14 Tayacan  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • Posts: 275
  • Joined: 18-January 11

Re: Reads each line from the file and supply it to the employee function

Posted 02 December 2012 - 02:59 PM

Just remove the last line - it tries to make a new employee from a list of employees, which doesn't really make sense ;)
Was This Post Helpful? 0
  • +
  • -

#15 TeaAnyOne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 02-December 12

Re: Reads each line from the file and supply it to the employee function

Posted 02 December 2012 - 05:19 PM

Im sorry to bug you again, but what do you mean by the last line? >.<
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2