Python equivalent of Fortran list-directed input

Viewed 356

I'd like to be able to read data from an input file in Python, similar to the way that Fortran handles a list-directed read (i.e. read (file, *) char_var, float_var, int_var).

The tricky part is that the way Fortran handles a read statement like this is very "forgiving" as far as the input format is concerned. For example, using the previous statement, this:

"some string" 10.0, 5

would be read the same as:

"some string",      10.0
5

and this:

"other string", 15.0 /

is read the same as:

"other string"
15
/

with the value of int_var retaining the same value as before the read statement. And trickier still this:

"nother string", , 7

will assign the values to char_var and int_var but float_var retains the same value as before the read statement.

Is there an elegant way to implement this?

2 Answers
Related