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 ?
mystring = '(1,2,3)'
HOW can I read the numbers in python ?
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 ?
[1, 2, 3]re.findall(r'(\d+)', '(1, 2, 3)') ['1', '2', '3']
map(int, re.findall(r'(\d+)', '(1, 2, 3)'))
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 ?
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.
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.
[1, 2, 3]>>> mystring = '(1,2,3)'
>>> mynumbers = [int(i) for i in mystring[1:-1].split(',')]
>>> mynumbers
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
Did you use the regex parens for a reason I am unaware of?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:
[1, 2, 3]re.findall(r'(\d+)', '(1, 2, 3)') ['1', '2', '3']
map(int, re.findall(r'(\d+)', '(1, 2, 3)'))
['1', '2', '3']>>> import re
>>> re.findall(r'(\d+)', '(1, 2, 3)') ['1', '2', '3']
>>> re.findall(r'\d+', '(1, 2, 3)')
Bengt said:Did you use the regex parens for a reason I am unaware of?
['1', '2', '3']import re
re.findall(r'(\d+)', '(1, 2, 3)') ['1', '2', '3']
re.findall(r'\d+', '(1, 2, 3)')
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.