On Mon, Feb 22, 2016 at 11:51 PM, BartC <b...@freeuk.com> wrote: > Yes, I mentioned that point. In the OP's language, the variables can be > declared as a particular type; in Python they could perhaps be 'declared' by > first assigning with 0, 0.0 or "" for example. But that would need reference > parameters to make use of tidily.
What you could do is something like this: def read_values(file, values): """Read one line from file and parse it into the given list. Prepopulate values with a series of types or instances of types. The line of text read will be parsed accordingly. Returns the number of elements successfully read. """ line = file.readline() for idx, val in enumerate(values): if not isinstance(val, type): val = type(val) # Now add the logic to read an int, a float, # a str, a Fraction, etc etc etc, from the line, # breaking out of the loop if one can't be # read, and stashing the result into values[idx] # if it can. return idx with open("inputfile") as f: values = [int, int, float, Fraction, str] while read_values(f, values) == 5: [index, level, percent, ratio, message] = values This lets you "declare" the values' types, and use an input/output parameter. It's not exactly the most Pythonic of techniques, but it does kinda work. I suspect, though, that what you'd _really_ want is something more like a sscanf string, or a "typed regex", which would allow you to specify a bit more flexibly what you're looking for. You could easily make a template string that has magic markers for "take an integer", which would insert [0-9]+ (or something more elaborate) and then capture it and call int() on the string before returning it. ChrisA -- https://mail.python.org/mailman/listinfo/python-list