473,485 Members | 1,473 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 ?
Jul 18 '05 #1
7 7601
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 && ma*@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
Jul 18 '05 #2
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.
Jul 18 '05 #3
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

Jul 18 '05 #4
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

Jul 18 '05 #5
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

Jul 18 '05 #6
On Thu, 17 Jul 2003 22:37:07 -0700, Erik Max Francis <ma*@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
Jul 18 '05 #7
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 && ma*@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
Jul 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1422
by: Daniel Yoo | last post by:
Hi everyone, I've implemented a scanf-like module in pure Python: http://hkn.eecs.berkeley.edu/~dyoo/python/scanf/ If you've ever had the itch to do something like:
8
3447
by: Steve Zimmerman | last post by:
This post is not intended as an argument to any other post, just some simple scanf experiments that I wanted to share. I found experiments 5 and 6 the most educational. Also, I thought...
7
19934
by: Przemo Drochomirecki | last post by:
hi, SCANF is not well described in my books, i'd like to do this stuff: 1) read all numbers from a line, separated by #32 2) read all numbers from a line, separaed by ',' 3) read whole line ...
5
3859
by: Eduardo Olivarez | last post by:
The following code does not work correctly on my machine. Either one of the scanf()'s alone work perfectly. However, when they are combined, the second scanf() call just reads what the first one...
7
7637
by: hugo27 | last post by:
obrhy8 June 18, 2004 Most compilers define EOF as -1. I'm just putting my toes in the water with a student's model named Miracle C. The ..h documentation of this compiler does state that when...
4
17143
by: sushant | last post by:
hi why do we use '&' operator in scanf like scanf("%d", &x); but why not in printf() like printf("%d" , x); thnx in advance sushant
17
3471
by: Lefty Bigfoot | last post by:
Hello, I am aware that a lot of people are wary of using scanf, because doing it improperly can be dangerous. I have tried to find a good tutorial on all the ins and outs of scanf() but been...
7
13241
by: gyan | last post by:
I want to read a line with white spaces though scanf. So i used: scanf("%",string); above is working in one program, but in other..what may be the reason?
14
13742
by: main() | last post by:
I know this is the problem that most newbies get into. #include<stdio.h> int main(void) { char a; scanf("%c",&a); /*1st scanf */ printf("%c\n",a); scanf("%c",&a); /*2nd scanf*/...
13
8996
by: AMD | last post by:
Hello, I often need to parse strings which contain a mix of characters, integers and floats, the C-language scanf function is very practical for this purpose. I've been looking for such a...
0
7087
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6958
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7158
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6821
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4856
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4546
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3058
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
593
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
244
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.