too many values with string.split
Marc 'BlackJack' Rintsch
bj_666 at gmx.net
Sat Sep 22 17:24:39 EDT 2007
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
More information about the Python-list
mailing list