scanf string in python

Jørgen Cederberg jorgencederberg at hotmail.com
Fri Jul 18 03:39:54 EDT 2003


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





More information about the Python-list mailing list