sscanf ?

Bruce Edge bedge at troikanetworks.com
Tue Nov 20 16:43:26 EST 2001


In <20011120222250.3156d74e.use-net at schabi.de>, Markus Schaber wrote:

> Hi,
> 
> Am Tue, 20 Nov 2001 09:17:21 -0800 schrieb Bruce Edge:
> 
>> Is there an equivalent function in python?
>> I know about all the regex stuff, but I have the format string that
>> was used to generate a string, which I'd like to use to decompose it
>> back into it's source data:
>> 
>> fmtstr = "%02d.%02d"
>> 
>> str = fmtstr % ( x, y )
>> 
>> # Now get x any y back using only str and fmtstr:
>> 
>> (x, y ) = ?
>> 
>> 
>> I suppose I could write something to convert printf style format
>> strings into regexp format, but it seems like there's gotta be an
>> easier way.
> 
> The problem is that there is no general solution for this problem.
> 
> How would you parse a text that was generated with a fmtstr like "%s%s"?
> 
> This is impossible. So there can't be a general scanstr function. C
> provides you with a crippled one, python provides you with string.split
> and regexp module.
> 
> 
> 
> Gruß,
> Markus
> 

Agreed, but sometimes a crippled one is all you need.

-Bruce.



More information about the Python-list mailing list