too many values with string.split

Discussion in 'Python' started by Shawn Minisall, Sep 22, 2007.

  1. I'm trying to unpack a list of 5 floats from a list read from a file and
    python is telling me 5 variables are too many for the string.split
    statement. Anyone have any other idea's? NOTE: the only reason I
    convert it to a float instead of just leaving it as a string in the loop
    is because I have to have it printed out as a float besides the names
    and then the average displayed underneath

    thx

    #read in data line by line
    for line in infile:
    mylist = string.split(line)
    firstName[counter] = mylist[0]
    lastName[counter] = mylist[1]
    grades[counter] = float(mylist[2])
    print firstName[counter],
    lastName[counter],":","\t\t",grades[counter]
    #increment counter
    counter = counter + 1

    #calculates and prints average score
    grades = str(grades)
    num1, num2, num3, num4, num5 = string.split(grades,",")
    average = float(num1 + num2 + num3 + num4 + num5) / 5
    print
    print "Average:"
     
    Shawn Minisall, Sep 22, 2007
    #1
    1. Advertising

  2. On Sat, 22 Sep 2007 17:00:47 -0400, Shawn Minisall wrote:

    > I'm trying to unpack a list of 5 floats from a list read from a file and
    > python is telling me 5 variables are too many for the string.split
    > statement.


    Please post the *real* message which I suspect is something like 'too many
    values to unpack', which is the other way around. The 5 names are not
    enough to take all the items from the split.

    > #read in data line by line
    > for line in infile:
    > mylist = string.split(line)


    Functions in the `string` module that are also available as methods on
    strings are deprecated.

    > firstName[counter] = mylist[0]
    > lastName[counter] = mylist[1]
    > grades[counter] = float(mylist[2])
    > print firstName[counter],
    > lastName[counter],":","\t\t",grades[counter]
    > #increment counter
    > counter = counter + 1


    Do you really need the counter? Can't you just append the values to the
    lists?

    > #calculates and prints average score
    > grades = str(grades)
    > num1, num2, num3, num4, num5 = string.split(grades,",")
    > average = float(num1 + num2 + num3 + num4 + num5) / 5


    This is very strange. You have a list of floats (I guess), convert that
    list to a string, split that string at commas, concatenate the *strings*
    between commas and then try to convert it to a `float`!? This is likely
    not what you want and should fail in most cases anyway.

    Ciao,
    Marc 'BlackJack' Rintsch
     
    Marc 'BlackJack' Rintsch, Sep 22, 2007
    #2
    1. Advertising

  3. Shawn Minisall

    Alain Guest

    Shawn Minisall a écrit :
    > I'm trying to unpack a list of 5 floats from a list read from a file and
    > python is telling me 5 variables are too many for the string.split
    > statement. Anyone have any other idea's? NOTE: the only reason I
    > convert it to a float instead of just leaving it as a string in the loop
    > is because I have to have it printed out as a float besides the names
    > and then the average displayed underneath
    >
    > thx
    >
    > #read in data line by line
    > for line in infile:
    > mylist = string.split(line)
    > firstName[counter] = mylist[0]
    > lastName[counter] = mylist[1]
    > grades[counter] = float(mylist[2])
    > print firstName[counter],
    > lastName[counter],":","\t\t",grades[counter]
    > #increment counter
    > counter = counter + 1
    >
    > #calculates and prints average score
    > grades = str(grades)
    > num1, num2, num3, num4, num5 = string.split(grades,",")
    > average = float(num1 + num2 + num3 + num4 + num5) / 5
    > print
    > print "Average:"


    As I can see, grades is a string that looks like '[12.0,12.0, ...]'

    So you can't split it just with string.split ()

    Rather than doing grades = str(grades) and split it,
    you have just to do :

    avarage = sum (grades) / len (grades)
     
    Alain, Sep 22, 2007
    #3
    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. peelman

    How many threads is too many?

    peelman, Jan 13, 2005, in forum: Java
    Replies:
    12
    Views:
    858
    Esmond Pitt
    Jan 15, 2005
  2. Rhino
    Replies:
    33
    Views:
    1,335
    Oliver Wong
    Dec 22, 2005
  3. rbt
    Replies:
    1
    Views:
    366
  4. How many roles is too many?

    , Jun 13, 2006, in forum: ASP .Net Security
    Replies:
    4
    Views:
    201
    Joe Kaplan \(MVP - ADSI\)
    Jun 18, 2006
  5. Dag Sunde
    Replies:
    4
    Views:
    373
Loading...

Share This Page