New to Python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • w

    #1

    New to Python

    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),
  • Thomas Krüger

    #2
    Re: New to Python

    w wrote:
    [color=blue]
    > 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.[/color]

    x = int(y)
    or
    x = float(y)
    ???

    Thomas

    Comment

    • Jimmie Webb

      #3
      Re: New to Python

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

      ValueError: Invalid literal for float(): EATE TA


      "Thomas Krüger" <thomas.krueger @gmx.net> wrote in message
      news:ciq1kk$1jf $05$2@news.t-online.com...[color=blue]
      > w wrote:
      >[color=green]
      > > 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.[/color]
      >
      > x = int(y)
      > or
      > x = float(y)
      > ???
      >
      > Thomas[/color]



      Comment

      • Marc 'BlackJack' Rintsch

        #4
        Re: New to Python

        In <YU%3d.179$Xy5. 164@fe61.usenet server.com>, Jimmie Webb wrote:
        [color=blue]
        > I tried x = float(nCost) but keep getting an error
        >
        > ValueError: Invalid literal for float(): EATE TA[/color]

        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

        Comment

        • Jimmie Webb

          #5
          Re: New to Python

          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_666@gmx.net > wrote in message
          news:pan.2004.0 9.21.20.25.53.8 79679@gmx.net.. .[color=blue]
          > In <YU%3d.179$Xy5. 164@fe61.usenet server.com>, Jimmie Webb wrote:
          >[color=green]
          > > I tried x = float(nCost) but keep getting an error
          > >
          > > ValueError: Invalid literal for float(): EATE TA[/color]
          >
          > 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[/color]



          Comment

          • Jere Kahanpaa

            #6
            Re: New to Python

            Jimmie Webb <webbsoft2@allt el.net> wrote:[color=blue]
            > I tried x = float(nCost) but keep getting an error
            > ValueError: Invalid literal for float(): EATE TA[/color]

            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

            Comment

            • Lee Harr

              #7
              Re: New to Python

              On 2004-09-21, w <webbsoft2@allt el.net> wrote:[color=blue]
              > 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
              >[/color]

              Put your print statement _before_ you do any calculations, so you
              can see what you are trying to calculate with ...


              [color=blue]
              >
              > 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),[/color]

              Comment

              • Peter Hansen

                #8
                Re: New to Python

                Jimmie Webb wrote:[color=blue]
                > 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?[/color]
                [color=blue][color=green][color=darkred]
                >>> float('EATA TE')[/color][/color][/color]
                Traceback (most recent call last):
                File "<stdin>", line 1, in ?
                ValueError: invalid literal for float(): EATA TE
                [color=blue][color=green][color=darkred]
                >>> float('this not a float!')[/color][/color][/color]
                Traceback (most recent call last):
                File "<stdin>", line 1, in ?
                ValueError: invalid literal for float(): this not a float!
                [color=blue][color=green][color=darkred]
                >>> float ('check your data file and recheck your code')[/color][/color][/color]
                Traceback (most recent call last):
                File "<stdin>", line 1, in ?
                ValueError: invalid literal for float(): check your data file and
                recheck your code


                -Peter

                Comment

                • Jimmie Webb

                  #9
                  Re: New to Python

                  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" <kahanpaa@sky1. astro.helsinki. fi> wrote in message
                  news:ciq37r$95m $1@oravannahka. helsinki.fi...[color=blue]
                  > Jimmie Webb <webbsoft2@allt el.net> wrote:[color=green]
                  > > I tried x = float(nCost) but keep getting an error
                  > > ValueError: Invalid literal for float(): EATE TA[/color]
                  >
                  > 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[/color]



                  Comment

                  • Jimmie Webb

                    #10
                    Re: New to Python

                    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" <webbsoft2@allt el.net> wrote in message
                    news:a1ff320f.0 409211158.29e62 56b@posting.goo gle.com...[color=blue]
                    > 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
                    >[/color]
                    ',0004.95,001,2 004260,0024.430 00,0000000,0000 0.00,000,000000 0,0000000,0004. 0
                    0000,2004262,20 04282),[color=blue]
                    > (00007756725433 ,'*BREYER PEACH ICE CR',0007,0006,' 56 OZ ','000224
                    >[/color]
                    ',0004.95,001,2 004260,0024.430 00,0000000,0000 0.00,000,000000 0,0000000,0004. 0
                    0000,2004262,20 04282),



                    Comment

                    Working...