Please help with problem creating class

Discussion in 'Python' started by auzarski2008@gmail.com, Apr 18, 2009.

  1. Guest

    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.
     
    , Apr 18, 2009
    #1
    1. Advertising

  2. MRAB Guest

    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?
     
    MRAB, Apr 18, 2009
    #2
    1. Advertising

  3. On 4/18/09, <> 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")
    >

    [...]

    >
    > 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.
     
    member thudfoo, Apr 18, 2009
    #3
  4. Correction:

    On 4/18/09, member thudfoo <> wrote:
    > On 4/18/09, <> 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")
    > >

    >
    > [...]
    >
    >
    > >
    > > 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.
    >
     
    member thudfoo, Apr 18, 2009
    #4
  5. wrote:

    >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
     
    Günther Dietrich, Apr 18, 2009
    #5
  6. Aahz Guest

    In article <>,
    =?UTF-8?Q?G=C3=BCnther?= Dietrich <> wrote:
    >
    >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/

    >>> w, x, y, z = '1\t2\t3\t4\t5'.split('\t')

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    ValueError: too many values to unpack
    --
    Aahz () <*> http://www.pythoncraft.com/

    "If you think it's expensive to hire a professional to do the job, wait
    until you hire an amateur." --Red Adair
     
    Aahz, Apr 18, 2009
    #6
  7. On Sat, 18 Apr 2009 12:06:54 -0700 (PDT),
    declaimed the following in gmane.comp.python.general:


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

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
     
    Dennis Lee Bieber, Apr 19, 2009
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    571
    Chris Uppal
    May 5, 2005
  2. E11
    Replies:
    1
    Views:
    4,955
    Thomas Weidenfeller
    Oct 12, 2005
  3. KK
    Replies:
    2
    Views:
    735
    Big Brian
    Oct 14, 2003
  4. MuZZy
    Replies:
    7
    Views:
    1,830
    Mike Hewson
    Jan 7, 2005
  5. mera
    Replies:
    7
    Views:
    1,159
Loading...

Share This Page