scanf string in python

Discussion in 'Python' started by lehrig, Jul 18, 2003.

  1. lehrig

    lehrig Guest

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

    mystring = '(1,2,3)'

    HOW can I read the numbers in python ?
    lehrig, Jul 18, 2003
    #1
    1. Advertising

  2. lehrig wrote:

    > I have a string which is returned by a C extension.
    >
    > mystring = '(1,2,3)'
    >
    > HOW can I read the numbers in python ?


    re.findall seems the safest and easiest solution:

    >>> re.findall(r'(\d+)', '(1, 2, 3)')

    ['1', '2', '3']
    >>> map(int, re.findall(r'(\d+)', '(1, 2, 3)'))

    [1, 2, 3]

    Flavor to taste.

    --
    Erik Max Francis && && 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
    Erik Max Francis, Jul 18, 2003
    #2
    1. Advertising

  3. lehrig

    lehrig Guest

    lehrig wrote:

    > I have a string which is returned by a C extension.
    >
    > mystring = '(1,2,3)'
    >
    > HOW can I read the numbers in python ?


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

    But there should be a more convenient solution.
    lehrig, Jul 18, 2003
    #3
  4. lehrig

    Karl Scalet Guest

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

    >
    >
    > Now I have done it like this:
    > tmp = mystring[1:-1]
    > tmplist = string.split(tmp,',')
    > x = int(tmplist[0])
    > y = int(tmplist[1])
    > z = int(tmplist[2])
    >
    > But there should be a more convenient solution.


    exec('result='+mystring)
    print result

    would be shorter

    Karl
    Karl Scalet, Jul 18, 2003
    #4
  5. lehrig wrote:
    > lehrig wrote:
    >
    >
    >>I have a string which is returned by a C extension.
    >>
    >>mystring = '(1,2,3)'
    >>
    >>HOW can I read the numbers in python ?

    >
    >
    > Now I have done it like this:
    > tmp = mystring[1:-1]
    > tmplist = string.split(tmp,',')
    > x = int(tmplist[0])
    > y = int(tmplist[1])
    > z = int(tmplist[2])
    >
    > But there should be a more convenient solution.


    Hi,

    some have suggested map, exec and re's. I came up with this list
    comprehenion

    >>> mystring = '(1,2,3)'
    >>> mynumbers = [int(i) for i in mystring[1:-1].split(',')]
    >>> mynumbers

    [1, 2, 3]

    regards
    Jorgen Cederberg
    =?ISO-8859-1?Q?J=F8rgen_Cederberg?=, Jul 18, 2003
    #5
  6. lehrig

    Andy Jewell Guest

    On Friday 18 Jul 2003 8:39 am, Jørgen Cederberg wrote:
    > lehrig wrote:
    > > lehrig wrote:
    > >>I have a string which is returned by a C extension.
    > >>
    > >>mystring = '(1,2,3)'
    > >>
    > >>HOW can I read the numbers in python ?

    > >
    > > Now I have done it like this:
    > > tmp = mystring[1:-1]
    > > tmplist = string.split(tmp,',')
    > > x = int(tmplist[0])
    > > y = int(tmplist[1])
    > > z = int(tmplist[2])
    > >
    > > But there should be a more convenient solution.

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

    >
    > [1, 2, 3]
    >
    > regards
    > Jorgen Cederberg


    what about:

    x,y,z=eval(mystring)

    ???
    see:

    >>> x,y,z=eval(mystring)
    >>> x,y,z

    (1, 2, 3)
    >>> x

    1
    >>> y

    2
    >>> z


    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 'ReallyNastyFunc()' instead of
    "(1,2,3)"... :-(. As long as you can guarantee the value won't be
    'dangerous' you'll be ok.

    hth -ndyj
    Andy Jewell, Jul 18, 2003
    #6
  7. On Thu, 17 Jul 2003 22:37:07 -0700, Erik Max Francis <> wrote:

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

    >
    >re.findall seems the safest and easiest solution:
    >
    >>>> re.findall(r'(\d+)', '(1, 2, 3)')

    >['1', '2', '3']
    >>>> map(int, re.findall(r'(\d+)', '(1, 2, 3)'))

    >[1, 2, 3]
    >

    Did you use the regex parens for a reason I am unaware of?

    >>> import re
    >>> re.findall(r'(\d+)', '(1, 2, 3)')

    ['1', '2', '3']
    >>> re.findall(r'\d+', '(1, 2, 3)')

    ['1', '2', '3']

    Regards,
    Bengt Richter
    Bengt Richter, Jul 18, 2003
    #7
  8. Bengt Richter wrote:

    > Did you use the regex parens for a reason I am unaware of?
    >
    > >>> import re
    > >>> re.findall(r'(\d+)', '(1, 2, 3)')

    > ['1', '2', '3']
    > >>> re.findall(r'\d+', '(1, 2, 3)')

    > ['1', '2', '3']


    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 && && 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
    Erik Max Francis, Jul 19, 2003
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JustSomeGuy

    string.scanf?

    JustSomeGuy, Jun 4, 2004, in forum: C++
    Replies:
    5
    Views:
    8,158
    Jorge Rivera
    Jun 6, 2004
  2. Radith
    Replies:
    18
    Views:
    564
    Michael Mair
    Aug 2, 2005
  3. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    scanf (yes/no) - doesn't work + deprecation errors scanf, fopen etc.

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Feb 16, 2006, in forum: C Programming
    Replies:
    185
    Views:
    3,345
    those who know me have no need of my name
    Apr 3, 2006
  4. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    difference between scanf("%i") and scanf("%d") ??? perhaps bug inVS2005?

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Apr 26, 2006, in forum: C Programming
    Replies:
    18
    Views:
    659
    Richard Bos
    May 2, 2006
  5. spaces in scanf format string

    , Nov 5, 2006, in forum: C Programming
    Replies:
    3
    Views:
    893
Loading...

Share This Page