string to integer problem

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

    string to integer problem

    Hi all,
    I'm currently having problems with data returned from a MySQL database in
    as much as the data returned is of the form '(1,10,23,33,35 ,48)' i.e. a
    list variable holding 6 integers returned as a string.
    I can't seem to get around how to return the data to it's original format
    i.e. the list with 6 integers - I have tried slicing the string and
    converting the charachters individually but then I run into trouble when I
    have integers over two digits wide as I can't combine the two separate
    digits back into the original number.
    I feel sure that someone has come across this before and I hope that my
    request for help on this will be answered.
    Thanks ,
    Lol McBride

  • Diez B. Roggisch

    #2
    Re: string to integer problem

    > I'm currently having problems with data returned from a MySQL database in[color=blue]
    > as much as the data returned is of the form '(1,10,23,33,35 ,48)' i.e. a
    > list variable holding 6 integers returned as a string.[/color]
    [color=blue][color=green][color=darkred]
    >>> s = '(1,10,23,33,35 ,48)'
    >>> t = eval(s)
    >>> print t[/color][/color][/color]
    (1,10,23,33,35, 48)

    --
    Regards,

    Diez B. Roggisch

    Comment

    • Peter Otten

      #3
      Re: string to integer problem

      Lol McBride wrote:
      [color=blue]
      > I'm currently having problems with data returned from a MySQL database in
      > as much as the data returned is of the form '(1,10,23,33,35 ,48)' i.e. a
      > list variable holding 6 integers returned as a string.
      > I can't seem to get around how to return the data to it's original format
      > i.e. the list with 6 integers - I have tried slicing the string and[/color]
      [color=blue][color=green][color=darkred]
      >>> s = '(1,10,23,33,35 ,48)'
      >>> [int(n) for n in s[1:-1].split(",")][/color][/color][/color]
      [1, 10, 23, 33, 35, 48]

      Peter

      Comment

      • Lol McBride

        #4
        Re: string to integer problem

        On Sun, 08 Feb 2004 16:29:14 +0000, Lol McBride wrote:
        [color=blue]
        > Hi all,
        > I'm currently having problems with data returned from a MySQL database in
        > as much as the data returned is of the form '(1,10,23,33,35 ,48)' i.e. a
        > list variable holding 6 integers returned as a string.
        > I can't seem to get around how to return the data to it's original format
        > i.e. the list with 6 integers - I have tried slicing the string and
        > converting the charachters individually but then I run into trouble when I
        > have integers over two digits wide as I can't combine the two separate
        > digits back into the original number.
        > I feel sure that someone has come across this before and I hope that my
        > request for help on this will be answered.
        > Thanks ,
        > Lol McBride[/color]
        Thank you for your replies - your help is very much appreciated.

        Comment

        • Lol McBride

          #5
          Re: string to integer problem

          On Sun, 08 Feb 2004 18:00:11 +0100, Diez B. Roggisch wrote:
          [color=blue][color=green]
          >> I'm currently having problems with data returned from a MySQL database in
          >> as much as the data returned is of the form '(1,10,23,33,35 ,48)' i.e. a
          >> list variable holding 6 integers returned as a string.[/color]
          >[color=green][color=darkred]
          >>>> s = '(1,10,23,33,35 ,48)'
          >>>> t = eval(s)
          >>>> print t[/color][/color]
          > (1,10,23,33,35, 48)[/color]
          Hi,
          thanks for this it worked a treat but I don't understand how - even after
          reading the docs for the eval() builtin function.Could you explain what's
          going on (in fairly simple terms as well - I'm a programmer for fun not
          cause it's my job)
          Thank you,
          Lol

          Comment

          • Diez B. Roggisch

            #6
            Re: string to integer problem

            > Hi,[color=blue]
            > thanks for this it worked a treat but I don't understand how - even after
            > reading the docs for the eval() builtin function.Could you explain what's
            > going on (in fairly simple terms as well - I'm a programmer for fun not
            > cause it's my job)
            > Thank you,
            > Lol[/color]

            eval is a function that simply evaluates the expression you pass to it as
            string - so

            eval("10 + 20")

            yields 30. You could think of eval beeing a call to something that starts
            the python interpreter as separete process/program, passes its arg as the
            script to execute, and returns the computed result - thats it.

            --
            Regards,

            Diez B. Roggisch

            Comment

            • Rich Krauter

              #7
              Re: string to integer problem

              On Sun, 2004-02-08 at 11:29, Lol McBride wrote:[color=blue]
              >I can't seem to get around how to return the data to it's original
              >format
              > i.e. the list with 6 integers - I have tried slicing the string and
              > converting the charachters individually but then I run into trouble[/color]
              when I[color=blue]
              > have integers over two digits wide as I can't combine the two separate
              > digits back into the original number.[/color]


              [color=blue][color=green][color=darkred]
              >>> l = '(1,10,23,33,35 ,48)'
              >>> [int(x) for x in l[1:-1].split(',')][/color][/color][/color]
              [1, 10, 23, 33, 35, 48]

              Probably not the best way, but it seems to work.

              Rich



              Comment

              • Gerrit

                #8
                Re: string to integer problem

                Lol McBride wrote:[color=blue]
                > On Sun, 08 Feb 2004 18:00:11 +0100, Diez B. Roggisch wrote:[color=green][color=darkred]
                > >>>> s = '(1,10,23,33,35 ,48)'
                > >>>> t = eval(s)
                > >>>> print t[/color]
                > > (1,10,23,33,35, 48)[/color]
                > Hi,
                > thanks for this it worked a treat but I don't understand how - even after
                > reading the docs for the eval() builtin function.Could you explain what's
                > going on (in fairly simple terms as well - I'm a programmer for fun not
                > cause it's my job)[/color]

                Passing a string to 'eval' is equal in having it as a part of an
                expression, for example as having in at the right side of an assignment
                operator (=), except for the quotes. The string returned happens to be
                valid Python syntax: numbers seperated by comma's with ( and ) around it
                create a tuple. It is valid to simply onter (1,2,3) at the Python
                interactive interpreter: this is why it is valid to pass '(1,2,3)' to
                eval. The result is the same.

                Hope this helps,
                yours,
                Gerrit.

                --
                PrePEP: Builtin path type

                Asperger's Syndrome - a personal approach:


                Comment

                • Anton Vredegoor

                  #9
                  Re: string to integer problem

                  Lol McBride <newspost@lolmc .com> wrote:
                  [color=blue]
                  >thanks for this it worked a treat but I don't understand how - even after
                  >reading the docs for the eval() builtin function.Could you explain what's
                  >going on (in fairly simple terms as well - I'm a programmer for fun not
                  >cause it's my job)[/color]

                  Sorry, but programmers for fun are expected to do better than
                  microslaves! Resist this professional nonsense.

                  Anton

                  Comment

                  Working...