scanf string in python

L

lehrig

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

mystring = '(1,2,3)'

HOW can I read the numbers in python ?
 
L

lehrig

lehrig said:
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.
 
K

Karl Scalet

lehrig said:
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?=

lehrig said:
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
 
A

Andy Jewell

lehrig said:
lehrig said:
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:

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
 
B

Bengt Richter

lehrig said:
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
 
E

Erik Max Francis

Bengt said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top