Please help with problem creating class

MRAB google at mrabarnett.plus.com
Sat Apr 18 15:34:10 EDT 2009


auzarski2008 at gmail.com wrote:
> Hi I have been working on a homework assignment that I am having a lot
> of trouble with.  I am so frustrated because every time I think I am
> getting close to figuring it out there is another problem.   If you
> could look at this and tell me what I am doing wrong I would very much
> appreciate it....
> 
> import string
> 
> from datetime import date
> 
> 
> 
> class Leaderapplicant:
> 
> 
> 
>         def __init__(self, line):
> 
>                 #convert the lines of data into fields and removes
> \n
> 
>                 line = line.rstrip("\n")
> 
>                 appname, leadername, start, end = line.split("\t")
> 
> 
> 
>                 self.appname = appname
> 
>                 self.leadername = leadername
> 
> 
> 
> 
> 
>                 yyyy, mm, dd = start.split(",")  #splits the string
> into dates
> 
>                 b = int(yyyy)
> 
>                 c = int(mm)
> 
>                 d = int(dd)
> 
>                 self.start = date(b,c,d)
> 
> 
> 
> 
> 
>                 yyyy, mm, dd = end.split(",")
> 
>                 b = int(yyyy)
> 
>                 c = int(mm)
> 
>                 d = int(dd)
> 
>                 self.end = date(b, c, d)
> 
> 
> 
> 
> 
>         def getAppname(self):
> 
>                 return self.appname
> 
> 
> 
>         def getLeadername(self):
> 
>                 return self.leadername
> 
> 
> 
>         def getStart(self):
> 
>                 return self.start
> 
> 
> 
>         def getEnd(self):
> 
>                 return self.end
> 
> 
> 
>         def getSomething(self):
> 
>         # infoStr is a tab separated line: name leadername startdate
> enddate
> 
> 
> 
>                 return self.appname + "\t" + self.leadername + "\t" +
> self.start + "\t" + self.end
> 
> 
> 
> #import Leader Applicant
> 
> 
> 
> def get_files():
>         infile = raw_input("What file is the data in? ")
>         outfile = raw_input("What file would you like the data to go
> in? ")
>         return infile, outfile
> 
> 
> 
> 
> def main():
> 
> 
> #recall get_files function
>         files = get_files()
> 
> #open files
>         infile = open(files[0], 'r')
>         outfile = open(files[1], 'w')
> 
> 
>         reportstart = raw_input("Please enter the date (yyyy, mm, dd)
> the reporting period started: ")
> 
>         yyyy, mm, dd = string.split(reportstart, ",")
> 
>         yyyy = int(yyyy)
> 
>         mm  = int(mm)
> 
>         dd  = int(dd)
> 
>         reportstartdate= date(yyyy, mm, dd)
> 
> 
> 
>         reportend = raw_input("Please enter the date (yyyy, mm, dd)
> the reporting period ended: ")
> 
>         yyyy, mm, dd = string.split(reportend, ",")
> 
>         yyyy = int(yyyy)
> 
>         mm = int(mm)
> 
>         dd = int(dd)
> 
>         reportenddate = date(yyyy, mm, dd)
> 
> 
> 
> 
> 
>         for line in infile:
> 
> 
> 
>                 a = Leaderapplicant(line) #from data file
> 
> 
> 
>                 if a.getEnd() >= reportstartdate and a.getEnd() <=
> reportenddate:
> 
> 
> 
>                         outfile.write(a.getAppname())
> 
>                         outfile.write
> ("\n")
> 
> 
> 
> 
> 
> #close files
> 
> 
> 
>                         infile.close()
> 
>                         outfile.close()
> 
> 
> 
>         #print "The list has been written to", files[1]
> 
> 
> 
> 
> 
> if __name__ == '__main__':
> 
>         main()
> 
> 
> 
> 
> 
> 
> I am using tab separated data in another file that looks like this...
> 
> 
> appname1   leadername1    2005, 02, 02    2006, 02, 02
> appname2   leadername2    2006, 03, 21    2007, 06, 28
> 
> etc...
> 
> The error message looks like this....
> 
>  back (most recent call last):
>   File "/home/amy/Documents/LIS452/assignment 3/testworks.py", line
> 97, in <module>
>     main()
>   File "/home/amy/Documents/LIS452/assignment 3/testworks.py", line
> 80, in main
>     a = Leaderapplicant(line) #from data file
>   File "/home/amy/Documents/LIS452/assignment 3/testworks.py", line 9,
> in __init__
>     appname, leadername, start, end = line.split("\t")
> ValueError: need more than 3 values to unpack
> 
> Any help would be greatly appreciated.  I have spent so much time on
> this that I am behind not only in this class but in other classes as
> well.
> 
Print out the value of 'line' using repr(). Is it what you expected?



More information about the Python-list mailing list