New to Python

Discussion in 'Python' started by w, Sep 21, 2004.

  1. w

    w Guest

    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( "", "r")

    while 1:
    data = in_file.readline()
    if not data:

    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

    (00007756725434,'*BREYER STRWBRY ICE ',0007,0006,' 56 OZ ','000216
    (00007756725433,'*BREYER PEACH ICE CR',0007,0006,' 56 OZ ','000224
    w, Sep 21, 2004
    1. Advertisements

  2. x = int(y)
    x = float(y)

    Thomas =?ISO-8859-1?Q?Kr=FCger?=, Sep 21, 2004
    1. Advertisements

  3. w

    Jimmie Webb Guest

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

    ValueError: Invalid literal for float(): EATE TA
    Jimmie Webb, Sep 21, 2004
  4. 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?

    Marc 'BlackJack' Rintsch
    Marc 'BlackJack' Rintsch, Sep 21, 2004
  5. w

    Jimmie Webb Guest

    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?

    Jimmie Webb, Sep 21, 2004
  6. 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 Kahanpaa, Sep 21, 2004
  7. w

    Lee Harr Guest

    Put your print statement _before_ you do any calculations, so you
    can see what you are trying to calculate with ...
    Lee Harr, Sep 21, 2004
  8. w

    Peter Hansen Guest

    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 Hansen, Sep 21, 2004
  9. w

    Jimmie Webb Guest

    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)
    Jimmie Webb, Sep 21, 2004
  10. w

    Jimmie Webb Guest

    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

    Jimmie Webb, Sep 23, 2004
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.