AMD schrieb:
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.
I'm pretty certain python won't grow an additional operator for this.
Yet you are free to create a scanf-implementation as 3rd-party-module.
IMHO the usability of the approach is very limited though. First of all,
the need to capture more than one input token is *very* seldom - nearly
all commandline-tools I know that do require interactive user-input
(like the linux kernel config tool) do so by providing either
line-by-line value entry (including defaults, something you can't do
with your approach), or even dialog-centric value entry with curses.
So - I doubt you will gather much momentum on this. Good luck though.
Diez
--
http://mail.python.org/mailman/listinfo/python-list