Am 21.02.16 um 14:16 schrieb BartC:
Even accepting that syntax limitations might require this to be written as:

   readline(f, a, b, c)

I can't see a straightforward way of making this possible while still
keeping a, b and c simple integer, float or string types (because
Python's reference parameters don't work quite the right way).

(There is also the question of 'readline' knowing what types of values
to read. This information would not be needed in Fortran or Basic but
somehow needs to be supplied here, if a particular set of types is to
imposed on the input.)

Are you sure that in Basic or Fortran the expected type is not supplied? I'm not too familiar with either, but I think that in Fortran the compiler deduces it from the (compile-time) static type of the variable, while in BASIC there used to be sigils (A$, A# etc.) to denote the type. A pythonic input function would look like this IMHO:

a,b,c = readline(f, int, float, str)

In other words, it seems this particular wheel does require re-inventing!

Yes, but the above seems quite trivial:

Apfelkiste:Tests chris$ cat parseline.py
def readline(f, *args):
        line=f.readline().split()
        return [type(x) for type,x in zip(args,line)]

with open("mydata.dat", "r") as f:
        ND, NINT, NT=readline(f, int, int, int)
        # next line holds NINT floats
        dincol=readline(f, *NINT*[float])
        # next line holds a string
        text=f.readline()
        
        print("Read: ", ND, NINT, NT)
        print(dincol)
        print(text)

Apfelkiste:Tests chris$ cat mydata.dat
 10            6             1
8.65  0.2192347   3.33E-4    44     0.0051        6
String
Apfelkiste:Tests chris$ python parseline.py
('Read: ', 10, 6, 1)
[8.65, 0.2192347, 0.000333, 44.0, 0.0051, 6.0]
String

Apfelkiste:Tests chris$

        Christian

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to