455,532 Members | 1,374 Online
Need help? Post your question and get tips & solutions from a community of 455,532 IT Pros & Developers. It's quick & easy.

# New to Python

 P: n/a excuse my ignorance. I cannot get this simple program to calculate the formula. The error message is unsupported operand type(s) str. I know the data that I'm reading from the file is a string but I cannot convert it to a float or int. The code a sample form the data file are below. # Calculate Gross Profit from SIL File import os.path in_file = open( "s3cga1.new", "r") while 1: data = in_file.readline() if not data: break if data[0:1] == "(": nCost = data[ 99:106] nPack = data[ 44:48] nRetail = data[ 79:87] nQuan = data[ 87:90] nAllowance = data[ 147:154] nGp = ( ( nRetail / nQuan ) - ( ( nCost - nAllowance) / nPack ) ) / ( nRetail / nQuan ) print nCost, nPack, nQuan, nRetail, nAllowance EXAMPLE OF DATA FILE (00007756725434,'*BREYER STRWBRY ICE ',0007,0006,' 56 OZ ','000216 ',0004.95,001,2004260,0024.43000,0000000,00000.00, 000,0000000,0000000,0004.00000,2004262,2004282), (00007756725433,'*BREYER PEACH ICE CR',0007,0006,' 56 OZ ','000224 ',0004.95,001,2004260,0024.43000,0000000,00000.00, 000,0000000,0000000,0004.00000,2004262,2004282), Jul 18 '05 #1
9 Replies

 P: n/a w wrote: excuse my ignorance.**I*cannot*get*this*simple*program*to*ca lculate the formula. The error message is unsupported operand type(s) str.**I*know*the*data that I'm reading from the file is a string but I cannot convert it to a float or int. x = int(y) or x = float(y) ??? Thomas Jul 18 '05 #2

 P: n/a I tried x = float(nCost) but keep getting an error ValueError: Invalid literal for float(): EATE TA "Thomas Krüger" wrote in message news:ci*************@news.t-online.com... w wrote: excuse my ignorance. I cannot get this simple program to calculate the formula. The error message is unsupported operand type(s) str. I know the data that I'm reading from the file is a string but I cannot convert it to a float or int. x = int(y) or x = float(y) ??? Thomas Jul 18 '05 #3

 P: n/a In , Jimmie Webb wrote: I tried x = float(nCost) but keep getting an error ValueError: Invalid literal for float(): EATE TA Maybe the slice values are just wrong? Have you printed all your variables after picking them from the string to make sure they contain what you expect? Ciao, Marc 'BlackJack' Rintsch Jul 18 '05 #4

 P: n/a Yes, They look good to me. The nCost Value is 0022.63. but I still get the error ValueError: Invalid literal for float(): EATE TA. What does the 'EATE TA' mean? Thanks! "Marc 'BlackJack' Rintsch" wrote in message news:pa****************************@gmx.net... In , Jimmie Webb wrote: I tried x = float(nCost) but keep getting an error ValueError: Invalid literal for float(): EATE TA Maybe the slice values are just wrong? Have you printed all your variables after picking them from the string to make sure they contain what you expect? Ciao, Marc 'BlackJack' Rintsch Jul 18 '05 #5

 P: n/a Jimmie Webb wrote: I tried x = float(nCost) but keep getting an error ValueError: Invalid literal for float(): EATE TA Come on, do some basic debugging! Insert 'print repr(nCost)' before the conversation and look at what you really are feeding to the float() function. Well, we already now it: at some point you feed the string 'EATE TA' to float(), and it obviously cannot convert this to a number. Jere -- Lord, make my words as sweet as honey, for one day I may have to eat them - Daryl Benson Jul 18 '05 #6

 P: n/a On 2004-09-21, w wrote: excuse my ignorance. I cannot get this simple program to calculate the formula. The error message is unsupported operand type(s) str. I know the data that I'm reading from the file is a string but I cannot convert it to a float or int. The code a sample form the data file are below. # Calculate Gross Profit from SIL File import os.path in_file = open( "s3cga1.new", "r") while 1: data = in_file.readline() if not data: break if data[0:1] == "(": nCost = data[ 99:106] nPack = data[ 44:48] nRetail = data[ 79:87] nQuan = data[ 87:90] nAllowance = data[ 147:154] nGp = ( ( nRetail / nQuan ) - ( ( nCost - nAllowance) / nPack ) ) / ( nRetail / nQuan ) print nCost, nPack, nQuan, nRetail, nAllowance Put your print statement _before_ you do any calculations, so you can see what you are trying to calculate with ... EXAMPLE OF DATA FILE (00007756725434,'*BREYER STRWBRY ICE ',0007,0006,' 56 OZ ','000216 ',0004.95,001,2004260,0024.43000,0000000,00000.00, 000,0000000,0000000,0004.00000,2004262,2004282), (00007756725433,'*BREYER PEACH ICE CR',0007,0006,' 56 OZ ','000224 ',0004.95,001,2004260,0024.43000,0000000,00000.00, 000,0000000,0000000,0004.00000,2004262,2004282), Jul 18 '05 #7

 P: n/a Jimmie Webb wrote: Yes, They look good to me. The nCost Value is 0022.63. but I still get the error ValueError: Invalid literal for float(): EATE TA. What does the 'EATE TA' mean? float('EATA TE') Traceback (most recent call last): File "", line 1, in ? ValueError: invalid literal for float(): EATA TE float('this not a float!') Traceback (most recent call last): File "", line 1, in ? ValueError: invalid literal for float(): this not a float! float ('check your data file and recheck your code') Traceback (most recent call last): File "", line 1, in ? ValueError: invalid literal for float(): check your data file and recheck your code -Peter Jul 18 '05 #8

 P: n/a Gee whiz. Don't jump down my throat for some oversite. It's not like I haven't put forth an effort trying to find out the problem before posting! Yeah, your right I was reading the "CREATE TABLE line out of the SIL file. Thanks (I think) "Jere Kahanpaa" wrote in message news:ci**********@oravannahka.helsinki.fi... Jimmie Webb wrote: I tried x = float(nCost) but keep getting an error ValueError: Invalid literal for float(): EATE TA Come on, do some basic debugging! Insert 'print repr(nCost)' before the conversation and look at what you really are feeding to the float() function. Well, we already now it: at some point you feed the string 'EATE TA' to float(), and it obviously cannot convert this to a number. Jere -- Lord, make my words as sweet as honey, for one day I may have to eat them - Daryl Benson Jul 18 '05 #9

 P: n/a Thanks to all the great helpful off-post comments that I received from this group. Glad to know there are some people out there that can be "friendly" and "helpful". It is nice to know some people will help and not call you lazy or stupid. Sorry, I just felt like I walked into the middle of a pit-bull attack. -Jimmie Webb "w" wrote in message news:a1**************************@posting.google.c om... excuse my ignorance. I cannot get this simple program to calculate the formula. The error message is unsupported operand type(s) str. I know the data that I'm reading from the file is a string but I cannot convert it to a float or int. The code a sample form the data file are below. # Calculate Gross Profit from SIL File import os.path in_file = open( "s3cga1.new", "r") while 1: data = in_file.readline() if not data: break if data[0:1] == "(": nCost = data[ 99:106] nPack = data[ 44:48] nRetail = data[ 79:87] nQuan = data[ 87:90] nAllowance = data[ 147:154] nGp = ( ( nRetail / nQuan ) - ( ( nCost - nAllowance) / nPack ) ) / ( nRetail / nQuan ) print nCost, nPack, nQuan, nRetail, nAllowance EXAMPLE OF DATA FILE (00007756725434,'*BREYER STRWBRY ICE ',0007,0006,' 56 OZ ','000216 ',0004.95,001,2004260,0024.43000,0000000,00000.00, 000,0000000,0000000,0004.0 0000,2004262,2004282), (00007756725433,'*BREYER PEACH ICE CR',0007,0006,' 56 OZ ','000224 ',0004.95,001,2004260,0024.43000,0000000,00000.00, 000,0000000,0000000,0004.0 0000,2004262,2004282), Jul 18 '05 #10

### This discussion thread is closed

Replies have been disabled for this discussion.