![]() |
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 ? |
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 |
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. |
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 |
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 |
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 |
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 |
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.