scanf string in python

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

    scanf string in python

    I have a string which is returned by a C extension.

    mystring = '(1,2,3)'

    HOW can I read the numbers in python ?
  • Erik Max Francis

    #2
    Re: scanf string in python

    lehrig wrote:
    [color=blue]
    > I have a string which is returned by a C extension.
    >
    > mystring = '(1,2,3)'
    >
    > HOW can I read the numbers in python ?[/color]

    re.findall seems the safest and easiest solution:
    [color=blue][color=green][color=darkred]
    >>> re.findall(r'(\ d+)', '(1, 2, 3)')[/color][/color][/color]
    ['1', '2', '3'][color=blue][color=green][color=darkred]
    >>> map(int, re.findall(r'(\ d+)', '(1, 2, 3)'))[/color][/color][/color]
    [1, 2, 3]

    Flavor to taste.

    --
    Erik Max Francis && [email protected] && http://www.alcyone.com/max/
    __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
    / \ You can buy any kind of love, but you can't buy love deluxe.
    \__/ Sade Adu

    Comment

    • lehrig

      #3
      Re: scanf string in python

      lehrig wrote:
      [color=blue]
      > I have a string which is returned by a C extension.
      >
      > mystring = '(1,2,3)'
      >
      > HOW can I read the numbers in python ?[/color]

      Now I have done it like this:
      tmp = mystring[1:-1]
      tmplist = string.split(tm p,',')
      x = int(tmplist[0])
      y = int(tmplist[1])
      z = int(tmplist[2])

      But there should be a more convenient solution.

      Comment

      • Karl Scalet

        #4
        Re: scanf string in python

        lehrig schrieb:[color=blue]
        > lehrig wrote:
        >
        >[color=green]
        >>I have a string which is returned by a C extension.
        >>
        >>mystring = '(1,2,3)'
        >>
        >>HOW can I read the numbers in python ?[/color]
        >
        >
        > Now I have done it like this:
        > tmp = mystring[1:-1]
        > tmplist = string.split(tm p,',')
        > x = int(tmplist[0])
        > y = int(tmplist[1])
        > z = int(tmplist[2])
        >
        > But there should be a more convenient solution.[/color]

        exec('result='+ mystring)
        print result

        would be shorter

        Karl

        Comment

        • Jørgen Cederberg

          #5
          Re: scanf string in python

          lehrig wrote:[color=blue]
          > lehrig wrote:
          >
          >[color=green]
          >>I have a string which is returned by a C extension.
          >>
          >>mystring = '(1,2,3)'
          >>
          >>HOW can I read the numbers in python ?[/color]
          >
          >
          > Now I have done it like this:
          > tmp = mystring[1:-1]
          > tmplist = string.split(tm p,',')
          > x = int(tmplist[0])
          > y = int(tmplist[1])
          > z = int(tmplist[2])
          >
          > But there should be a more convenient solution.[/color]

          Hi,

          some have suggested map, exec and re's. I came up with this list
          comprehenion
          [color=blue][color=green][color=darkred]
          >>> mystring = '(1,2,3)'
          >>> mynumbers = [int(i) for i in mystring[1:-1].split(',')]
          >>> mynumbers[/color][/color][/color]
          [1, 2, 3]

          regards
          Jorgen Cederberg

          Comment

          • Andy Jewell

            #6
            Re: scanf string in python

            On Friday 18 Jul 2003 8:39 am, Jørgen Cederberg wrote:[color=blue]
            > lehrig wrote:[color=green]
            > > lehrig wrote:[color=darkred]
            > >>I have a string which is returned by a C extension.
            > >>
            > >>mystring = '(1,2,3)'
            > >>
            > >>HOW can I read the numbers in python ?[/color]
            > >
            > > Now I have done it like this:
            > > tmp = mystring[1:-1]
            > > tmplist = string.split(tm p,',')
            > > x = int(tmplist[0])
            > > y = int(tmplist[1])
            > > z = int(tmplist[2])
            > >
            > > But there should be a more convenient solution.[/color]
            >
            > Hi,
            >
            > some have suggested map, exec and re's. I came up with this list
            > comprehenion
            >[color=green][color=darkred]
            > >>> mystring = '(1,2,3)'
            > >>> mynumbers = [int(i) for i in mystring[1:-1].split(',')]
            > >>> mynumbers[/color][/color]
            >
            > [1, 2, 3]
            >
            > regards
            > Jorgen Cederberg[/color]

            what about:

            x,y,z=eval(myst ring)

            ???
            see:
            [color=blue][color=green][color=darkred]
            >>> x,y,z=eval(myst ring)
            >>> x,y,z[/color][/color][/color]
            (1, 2, 3)[color=blue][color=green][color=darkred]
            >>> x[/color][/color][/color]
            1[color=blue][color=green][color=darkred]
            >>> y[/color][/color][/color]
            2[color=blue][color=green][color=darkred]
            >>> z[/color][/color][/color]

            NOTE: this could introduce exploitable behaviour if you can't guarantee that
            the string is *only* going to contain a tuple of nembers... think about what
            could happen if the c code returned 'ReallyNastyFun c()' instead of
            "(1,2,3)".. . :-(. As long as you can guarantee the value won't be
            'dangerous' you'll be ok.

            hth -ndyj



            Comment

            • Bengt Richter

              #7
              Re: scanf string in python

              On Thu, 17 Jul 2003 22:37:07 -0700, Erik Max Francis <[email protected] m> wrote:
              [color=blue]
              >lehrig wrote:
              >[color=green]
              >> I have a string which is returned by a C extension.
              >>
              >> mystring = '(1,2,3)'
              >>
              >> HOW can I read the numbers in python ?[/color]
              >
              >re.findall seems the safest and easiest solution:
              >[color=green][color=darkred]
              >>>> re.findall(r'(\ d+)', '(1, 2, 3)')[/color][/color]
              >['1', '2', '3'][color=green][color=darkred]
              >>>> map(int, re.findall(r'(\ d+)', '(1, 2, 3)'))[/color][/color]
              >[1, 2, 3]
              >[/color]
              Did you use the regex parens for a reason I am unaware of?
              [color=blue][color=green][color=darkred]
              >>> import re
              >>> re.findall(r'(\ d+)', '(1, 2, 3)')[/color][/color][/color]
              ['1', '2', '3'][color=blue][color=green][color=darkred]
              >>> re.findall(r'\d +', '(1, 2, 3)')[/color][/color][/color]
              ['1', '2', '3']

              Regards,
              Bengt Richter

              Comment

              • Erik Max Francis

                #8
                Re: scanf string in python

                Bengt Richter wrote:
                [color=blue]
                > Did you use the regex parens for a reason I am unaware of?
                >[color=green][color=darkred]
                > >>> import re
                > >>> re.findall(r'(\ d+)', '(1, 2, 3)')[/color][/color]
                > ['1', '2', '3'][color=green][color=darkred]
                > >>> re.findall(r'\d +', '(1, 2, 3)')[/color][/color]
                > ['1', '2', '3'][/color]

                Habit. In any other context, I'd want to isolate those buggers in a
                group, so that's what I wrote that here. I wasn't specifically aware
                that they were unnecessary with re.findall.

                --
                Erik Max Francis && [email protected] && http://www.alcyone.com/max/
                __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
                / \ I'm sharing the joy / I'm glowing like sunshine
                \__/ Chante Moore

                Comment

                Working...