A
AMD
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 feature and I have been quite surprised to
find that it has been discussed as far back as 2001 but never
implemented. The recommended approach seems to be to use split and then
atoi or atof or to use regex and then atoi and atof. Both approaches
seem to be a lot less natural and much more cumbersome than scanf. If
python already has a % string operator that behaves like printf, why not
implement either a %% or << string operator to behave like scanf, use
could be like the followng:
a, b, c = "%d %f %5c" %% "1 2.0 abcde"
or
a, b, c = "%d %f %5c" << "1 2.0 abcde"
%% is closer to the % operator
<< seems more intuitive to me
either of this methods seems to me much simpler than:
lst = "1 2;0 abcde".split()
a = int(lst[0])
b = float(lst[1])
c = lst[2]
or even worse when using regular expressions to parse such simple input.
I like python because it is concise and easy to read and I really think
it could use such an operator.
I know this has been discussed before and many times, but all previous
threads I found seem to be dead and I would like to invite further
investigation of this topic.
Cheers,
André M. Descombes
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 feature and I have been quite surprised to
find that it has been discussed as far back as 2001 but never
implemented. The recommended approach seems to be to use split and then
atoi or atof or to use regex and then atoi and atof. Both approaches
seem to be a lot less natural and much more cumbersome than scanf. If
python already has a % string operator that behaves like printf, why not
implement either a %% or << string operator to behave like scanf, use
could be like the followng:
a, b, c = "%d %f %5c" %% "1 2.0 abcde"
or
a, b, c = "%d %f %5c" << "1 2.0 abcde"
%% is closer to the % operator
<< seems more intuitive to me
either of this methods seems to me much simpler than:
lst = "1 2;0 abcde".split()
a = int(lst[0])
b = float(lst[1])
c = lst[2]
or even worse when using regular expressions to parse such simple input.
I like python because it is concise and easy to read and I really think
it could use such an operator.
I know this has been discussed before and many times, but all previous
threads I found seem to be dead and I would like to invite further
investigation of this topic.
Cheers,
André M. Descombes