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]
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])
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]
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]
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]
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.
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']
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.
Comment