Is there any way in Python to reverse the formating operation done through the “%” operator ?
formated = "%d ooo%s" % (12, "ps")
#formated is now '12 ooops'
(arg1, arg2) = theFunctionImSeeking("12 ooops", "%d ooo%s")
#arg1 is 12 and arg2 is "ps"
EDIT Regexp can be a solution for that but they are harder to write and I suspect them to be slower since they can handle more complex structures. I would really like an equivalent to sscanf.
Use regular expressions (
remodule):Regular expressions is as near as you can get to do what you want. There is no way to do it using the same format string (
'%d ooo%s').EDIT: As @Daenyth suggested, you could implement your own function with this behaviour:
Usage:
Of course,
python_scanfwon’t work with more complex patterns like%.4for%r.