string to list of numbers conversion

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jm.suresh@no.spam.gmail.com

    #1

    string to list of numbers conversion

    Hi,
    I have a string '((1,2), (3,4))' and I want to convert this into a
    python tuple of numbers. But I do not want to use eval() because I do
    not want to execute any code in that string and limit it to list of
    numbers.
    Is there any alternative way?

    Thanks.
    Suresh

  • Gerard Flanagan

    #2
    Re: string to list of numbers conversion


    jm.suresh@no.sp am.gmail.com wrote:
    Hi,
    I have a string '((1,2), (3,4))' and I want to convert this into a
    python tuple of numbers. But I do not want to use eval() because I do
    not want to execute any code in that string and limit it to list of
    numbers.
    Is there any alternative way?

    Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
    (Intel)] on win
    32
    Type "help", "copyright" , "credits" or "license" for more information.
    >>s = '((1,2), (3,4))'
    >>s = filter(lambda char: char not in ')(', s)
    >>s
    '1,2, 3,4'
    >>s = s.split(',')
    >>s
    ['1', '2', ' 3', '4']
    >>s = map(float, s)
    >>s
    [1.0, 2.0, 3.0, 4.0]
    >>t1 = s[::2]
    >>t1
    [1.0, 3.0]
    >>t2 = s[1::2]
    >>t2
    [2.0, 4.0]
    >>zip(t1, t2)
    [(1.0, 2.0), (3.0, 4.0)]
    >>>
    Gerard

    Comment

    • Peter Otten

      #3
      Re: string to list of numbers conversion

      jm.suresh@no.sp am.gmail.com wrote:
      I have a string '((1,2), (3,4))' and I want to convert this into a
      python tuple of numbers. But I do not want to use eval() because I do
      not want to execute any code in that string and limit it to list of
      numbers.
      Is there any alternative way?


      Peter

      Comment

      • jm.suresh@no.spam.gmail.com

        #4
        Re: string to list of numbers conversion

        Peter, Thanks.

        This recipe fails when negative numbers are used.

        safe_eval('(12, -12)')
        *** Unsafe_Source_E rror: Line 1. Unsupported source construct:
        compiler.ast.Un arySub

        But, I think it could be easily fixed for somebody who understands the
        script. Can somebody help.

        Thanks.
        Suresh
        Peter Otten wrote:
        jm.suresh@no.sp am.gmail.com wrote:
        >
        I have a string '((1,2), (3,4))' and I want to convert this into a
        python tuple of numbers. But I do not want to use eval() because I do
        not want to execute any code in that string and limit it to list of
        numbers.
        Is there any alternative way?
        >

        >
        Peter

        Comment

        • bearophileHUGS@lycos.com

          #5
          Re: string to list of numbers conversion


          jm.suresh@no.sp am.gmail.com wrote:
          Hi,
          I have a string '((1,2), (3,4))' and I want to convert this into a
          python tuple of numbers. But I do not want to use eval() because I do
          not want to execute any code in that string and limit it to list of
          numbers.
          Is there any alternative way?
          This is a possibile solution, no input errors are taken into account:
          >>s = '((1,2), (3,4), (-5,9.2))'
          >>from string import maketrans
          >>tab = maketrans("(), ", " "*4)
          >>s.translate(t ab)
          ' 1 2 3 4 -5 9.2 '
          >>l = s.translate(tab ).split()
          >>l
          ['1', '2', '3', '4', '-5', '9.2']
          >>l2 = map(float, l)
          >>l2
          [1.0, 2.0, 3.0, 4.0, -5.0, 9.1999999999999 993]
          >># This is partition(l2, 2)
          >>[l2[i:i+2] for i in xrange(0, len(l2), 2)]
          [[1.0, 2.0], [3.0, 4.0], [-5.0, 9.1999999999999 993]]

          Bye,
          bearophile

          Comment

          • Peter Otten

            #6
            Re: string to list of numbers conversion

            jm.suresh@no.sp am.gmail.com wrote:
            This recipe fails when negative numbers are used.
            >
            safe_eval('(12, -12)')
            *** Unsafe_Source_E rror: Line 1. Unsupported source construct:
            compiler.ast.Un arySub
            >
            But, I think it could be easily fixed for somebody who understands the
            script.
            I think that somebody could be you.
            Can somebody help.
            Start with

            class SafeEval(object ):
            # ...
            def visitUnarySub(s elf, node, **kw):
            return -node.expr.value

            and then add some error handling.

            Peter

            Comment

            • Frederic Rentsch

              #7
              Re: string to list of numbers conversion

              jm.suresh@no.sp am.gmail.com wrote:
              Hi,
              I have a string '((1,2), (3,4))' and I want to convert this into a
              python tuple of numbers. But I do not want to use eval() because I do
              not want to execute any code in that string and limit it to list of
              numbers.
              Is there any alternative way?
              >
              Thanks.
              Suresh
              >
              >
              s = '((1,2), (3,4))'
              separators = re.compile ('\(\s*\(|\)\s* ,\s*\(|\)\s*\)' )
              tuple ([(float (n[0]), float (n[1])) for n in [pair.split (',') for pair
              in separators.spli t (s) if pair]])
              ((1.0, 2.0), (3.0, 4.0))

              Frederic


              Comment

              • Paul McGuire

                #8
                Re: string to list of numbers conversion

                "jm.suresh@no.s pam.gmail.com" <jm.suresh@gmai l.comwrote in message
                news:1162730071 .953080.284980@ i42g2000cwa.goo glegroups.com.. .
                Hi,
                I have a string '((1,2), (3,4))' and I want to convert this into a
                python tuple of numbers. But I do not want to use eval() because I do
                not want to execute any code in that string and limit it to list of
                numbers.
                Is there any alternative way?
                >
                Thanks.
                Suresh
                >
                Pyparsing comes with an example that parses strings representing lists.
                Here's that example, converted to parsing only tuples of numbers. Note that
                this does not presume that tuples are only pairs, but can be any number of
                numeric values, nested to any depth, and with arbitrary whitespace, etc.
                This grammar also includes converters by type, so that ints come out as
                ints, and floats as floats. (This grammar doesn't handle empty tuples, but
                it does handle tuples that include an extra ',' after the last tuple
                element.)

                -- Paul
                Download pyparsing at http://sourceforge.net/projects/pyparsing/ .


                from pyparsing import *

                integer = (Word(nums)|Wor d('-+',nums)).setNa me("integer")
                real = Combine(integer + "." + Optional(Word(n ums))).setName( "real")
                tupleStr = Forward().setNa me("tuple")
                tupleItem = real | integer | tupleStr
                tupleStr << ( Suppress("(") + delimitedList(t upleItem) +
                Optional(Suppre ss(",")) + Suppress(")") )

                # add parse actions to do conversion during parsing
                integer.setPars eAction( lambda toks: int(toks[0]) )
                real.setParseAc tion( lambda toks: float(toks[0]) )
                tupleStr.setPar seAction( lambda toks: tuple(toks) )

                s = '((1,2), (3,4), (-5,9.2),)'
                print tupleStr.parseS tring(s)[0]

                Gives:
                ((1, 2), (3, 4), (-5, 9.1999999999999 993))


                Comment

                • Fredrik Lundh

                  #9
                  Re: string to list of numbers conversion

                  jm.suresh@no.sp am.gmail.com wrote:
                  I have a string '((1,2), (3,4))' and I want to convert this into a
                  python tuple of numbers. But I do not want to use eval() because I do
                  not want to execute any code in that string and limit it to list of
                  numbers.
                  here's yet another approach:



                  also see:



                  </F>

                  Comment

                  • henning

                    #10
                    Re: string to list of numbers conversion

                    jm.suresh@no.sp am.gmail.com skrev:
                    Hi,
                    I have a string '((1,2), (3,4))' and I want to convert this into a
                    python tuple of numbers.
                    I think your question is deeper and more natural than is clear from the
                    many recepies given so far in this thread, so I'll take on another
                    point of view,
                    >From a language design perspective, there is no reason why not the
                    parsing capacity of the Python interpreter would be accessible in a
                    modular fashion to the user/programmer. E.g used like this:

                    I an imaginable Python, define you expect for an answer. In this case:
                    (1)
                    # import junctions, types from maybefuture:-)
                    string = ((1,2), (3,4))
                    type a = tuple a | int
                    myTuple = eval(string, goal=a)


                    Obviously, if you expect _only_ the given form, then this might be
                    better:

                    (2)
                    # import types from maybefuture:-)
                    type a = ((int,int),(int ,int))
                    myTuple = eval(string, goal=a)

                    Note the use of a "a|b" in line 2 (I think Perl 6 is among the few
                    programming languages giving a reasonable semantics to junctions so
                    far).

                    Version 2 above sholud not be a big addition to Python conceptually.
                    Motivation:
                    It is easy to think clearly about.
                    It makes it easier to use eval safely and makes code more readable.

                    This is a topic of interest to me, so feel free to post either on list
                    or directly to me.

                    Thanks/Henning

                    Comment

                    Working...