There is no Python equivalent of C/C++ fscanf or MATLAB fscanf, sscanf or textscan.Here are some alternatives that I have found.
- numpy.genfromtext() does more or less exactly the same thing. It reads strings, via StringIO, and file. There is a nice section on importing data in the NumPyUser Guide.
- instead of format specifiers like '%8f%4s%2d' use delimiter=(8, 4, 2) and set dtype=(float, str, int). Voila!
- But genfromtext() does so much more! Using dtypes you can also set field names. There are options for skipping headers and footers, See the documentation.
- csv module from the standard Python reference
- numpy.loadtxt() which has the added advantage of reading in data as NumPy arrays.
- str.split() obviously
There are probably many other methods, but for MATLAB converts, once they move from disbelief and denial onto acceptance, it's pretty straightforward issue to resolve.