Emulating Pascal input
Jeremy Yallop
jeremy at jdyallop.freeserve.co.uk
Wed May 22 08:08:02 EDT 2002
* Michael Williams
| Having read through the many threads on this topic on comp.lang.python I
| realise and understand that modern programs' input generally requires
| thought and customization from the program author. However, we strongly
| feel there should be an equivalent to Pascal's readln (which is
| basically C's scanf but without the formatting strings) for simple,
| generally numerical, whitespace-separated input.
Would something like the following be of any use? It returns a tuple
of values, read from a file, converted to int, float, or string as
appropriate. I'm afraid I don't know any Pascal, so I'm not sure if
the behaviour is exactly what you want.
import sys
def readln(fin=sys.stdin):
if fin is sys.stdin:
line = raw_input()
else:
line = fin.readline()
rv = ()
for item in line.split():
try:
rv += (int(item),)
except ValueError:
try:
rv += (float(item),)
except ValueError:
rv += (item,)
return rv
Jeremy.
More information about the Python-list
mailing list