473,396 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 7593
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
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
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.