Please help with problem creating class

A

auzarski2008

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.
 
M

MRAB

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?
 
M

member thudfoo

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")
[...]


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.

Immediately before line 9 put the following line:

print line.split("\t")

Now run it again and see how many values are in the list that is
printed. Your code expects that there will be exactly three.
 
M

member thudfoo

Correction:

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")

[...]



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.


Immediately before line 9 put the following line:

print line.split("\t")

Now run it again and see how many values are in the list that is
printed. Your code expects that there will be exactly four.
 
G

Günther Dietrich

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

You assign the result of line.split() to four variables. So split needs
at least four fields (that's more than three) in line to process.

Examine your input data ("appname1 leadername1 2005..."), if it
_really_ contains alt least three tab characters to separate the data
fields.
Maybe you created the input file by means of the same editor you use for
coding your python code, so the probability is high, that the editor
produces multiple space characters when you press the tab key.
You could load the input file into vi or vim and give vi/vim the command

:set list

In list mode, tabs are rendered as '^I' (without the quotes).

Or you use a hex editor or something like od to inspect the input file.



Best regards,

Günther
 
A

Aahz

You assign the result of line.split() to four variables. So split needs
at least four fields (that's more than three) in line to process.

s/at least/exactly/
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: too many values to unpack
 
D

Dennis Lee Bieber

I am using tab separated data in another file that looks like this...
Are you sure you have tabs IN the file? And not tab-simulation via
space insertion?

The second comment I'd have would be: If it is TSV, why not use the
csv module with proper options to read the file and parse out the
fields?

.... instead of trying to catch all the corner cases by hand...
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top