473,804 Members | 2,719 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.readlin e()
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,2 004260,0024.430 00,0000000,0000 0.00,000,000000 0,0000000,0004. 00000,2004262,2 004282),
(00007756725433 ,'*BREYER PEACH ICE CR',0007,0006,' 56 OZ ','000224
',0004.95,001,2 004260,0024.430 00,0000000,0000 0.00,000,000000 0,0000000,0004. 00000,2004262,2 004282),
Jul 18 '05 #1
9 2933
w wrote:
excuse my ignorance.**I*c annot*get*this* simple*program* to*calculate
the formula.

The error message is unsupported operand type(s) str.**I*know*th e*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.usenet server.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.usenet server.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*******@allt el.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*******@allt el.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.readlin e()
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,2 004260,0024.430 00,0000000,0000 0.00,000,000000 0,0000000,0004. 00000,2004262,2 004282),
(00007756725433 ,'*BREYER PEACH ICE CR',0007,0006,' 56 OZ ','000224
',0004.95,001,2 004260,0024.430 00,0000000,0000 0.00,000,000000 0,0000000,0004. 00000,2004262,2 004282),

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*******@allt el.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*******@allt el.net> wrote in message
news:a1******** *************** ***@posting.goo gle.com...
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.readlin e()
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,2 004260,0024.430 00,0000000,0000 0.00,000,000000 0,0000000,0004. 0
0000,2004262,20 04282), (00007756725433 ,'*BREYER PEACH ICE CR',0007,0006,' 56 OZ ','000224

',0004.95,001,2 004260,0024.430 00,0000000,0000 0.00,000,000000 0,0000000,0004. 0
0000,2004262,20 04282),

Jul 18 '05 #10

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

Similar topics

0
9577
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10569
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10315
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9140
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6847
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4295
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.