Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   scanf string in python (http://www.velocityreviews.com/forums/t319842-scanf-string-in-python.html)

lehrig 07-18-2003 05:00 AM

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 07-18-2003 05:37 AM

Re: scanf string in python
 
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 && max@alcyone.com && 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

lehrig 07-18-2003 05:45 AM

Re: scanf string in python
 
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.

Karl Scalet 07-18-2003 07:21 AM

Re: scanf string in python
 
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


=?ISO-8859-1?Q?J=F8rgen_Cederberg?= 07-18-2003 07:39 AM

Re: scanf string in python
 
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


Andy Jewell 07-18-2003 12:46 PM

Re: scanf string in python
 
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




Bengt Richter 07-18-2003 08:15 PM

Re: scanf string in python
 
On Thu, 17 Jul 2003 22:37:07 -0700, Erik Max Francis <max@alcyone.com> 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

Erik Max Francis 07-19-2003 03:14 AM

Re: scanf string in python
 
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 && max@alcyone.com && 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


All times are GMT. The time now is 07:29 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57