473,386 Members | 1,817 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.

New to Python

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),
Jul 18 '05 #1
9 2908
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
I tried x = float(nCost) but keep getting an error

ValueError: Invalid literal for float(): EATE TA
"Thomas Krüger" <th************@gmx.net> 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
In <YU***************@fe61.usenetserver.com>, 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
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" <bj****@gmx.net> wrote in message
news:pa****************************@gmx.net...
In <YU***************@fe61.usenetserver.com>, 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
Jimmie Webb <we*******@alltel.net> 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
On 2004-09-21, w <we*******@alltel.net> 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
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 "<stdin>", line 1, in ?
ValueError: invalid literal for float(): EATA TE
float('this not a float!') Traceback (most recent call last):
File "<stdin>", 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 "<stdin>", line 1, in ?
ValueError: invalid literal for float(): check your data file and
recheck your code
-Peter
Jul 18 '05 #8
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" <ka******@sky1.astro.helsinki.fi> wrote in message
news:ci**********@oravannahka.helsinki.fi...
Jimmie Webb <we*******@alltel.net> 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
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" <we*******@alltel.net> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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:
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

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.