New to Python

W

w

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),
 
T

Thomas =?ISO-8859-1?Q?Kr=FCger?=

w said:
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
 
J

Jimmie Webb

I tried x = float(nCost) but keep getting an error

ValueError: Invalid literal for float(): EATE TA
 
M

Marc 'BlackJack' Rintsch

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
 
J

Jimmie Webb

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

Jere Kahanpaa

Jimmie Webb said:
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
 
L

Lee Harr

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

Peter Hansen

Jimmie said:
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?
Traceback (most recent call last):
Traceback (most recent call last):
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: invalid literal for float(): check your data file and
recheck your code


-Peter
 
J

Jimmie Webb

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

Jimmie Webb

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 said:
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),
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top