473,386 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

too many values with string.split

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:"
Sep 22 '07 #1
2 3732
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
Sep 22 '07 #2
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)
Sep 22 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: spradl | last post by:
Anyone have a VB function or know how to retrieve original querystring values after they have been decryted and lie within a string? For example: Say I have the querystring...
7
by: Jim Adamson | last post by:
I have created a web page that receives names and values from a URL string of another page e.g. http://hostname/resolve?sublibrary=JMLibrary&collection=Elton&shelfmark=LM 36TY ... and decodes the...
19
by: nazgulero | last post by:
Hello all, I wonder if anybody can give me a hint about what I have to do to get this working: I am creating a drop down box using the script below. The result is two text fields; now I want...
3
by: Elezar Simeon Papo | last post by:
Hello All, I have a tab separated input file (data.txt) in text format - the file looks like this SCHOOL DEPART1 DEPART2 DEPART3 Harvard Economics Mathematics Physics...
8
by: Chris A via AccessMonster.com | last post by:
I have an interesting problem that I have yet to come accross that I can't change data structure on because it is an export from filemaker I am reformatting for another dept. anyway. I have a table...
3
by: Eric_Dexter | last post by:
I almost have this thing running like I want it to run but I want the values to come from the program that calls this one. There are two things I want to pass File_Name and CutString. They both...
8
by: Shawn Minisall | last post by:
I am trying to read a few lines of a file with multiple values, the rest are single and are reading in fine. With the multiple value lines, python says this "ValueError: too many values to...
3
by: Shawn Minisall | last post by:
I am trying to read a few lines of a file with multiple values, the rest are single and are reading in fine. With the multiple value lines, python says this "ValueError: too many values to...
0
debasisdas
by: debasisdas | last post by:
The following sample code is designed to display the use of accepting a list of values in a single parameter and process the same in the where clause inside a procedure. STEP1:-First create an...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.