scanf in python

AMD amdescombes at gmail.com
Mon Jul 21 21:45:52 CEST 2008


> 
> 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

Actually it is quite common, it is used for processing of files not for 
reading parameters. You can use it whenever you need to read a simple 
csv file or fixed format file which contains many lines with several 
fields per line.
The advantage of the approach is that it combines the parsing and 
conversion of the fields into one operation.
Another advantage of using simple formatting strings is that it allows 
for easy translation of these lines, just like you have with the % 
operator for output. I don't see why python can have an operator for 
output but it can't have one for input, it's just not symmetrical.
I don´t see why you can't use this method for line-by-line value entry, 
just add \n between your %s or %d.
The method is quite versatile and much simpler than regular expressions 
plus conversion afterwards.

André



More information about the Python-list mailing list