The Matlab function fscanf()
seems to be very powerful. Is there any equivalent of the same in python (or numpy)?
Specifically I want to read a matrix from file but I don't want to iterate through each line to read the matrix. Something of this sort (from matlab for reading a 2D 1000x1000 matrix):
matrix = fscanf(fopen('input.txt'),'%d',[1000,1000]);
I'm pretty sure there is not, but iterating isn't too hard. This would do it:
If you need something more complex (i.e. not just ints separated by single characters), regular expressions may be the way to go.
Have you taken a look at numpy? - http://www.scipy.org/Download
By the way, fscanf internally stores the data in column order - So I don't think there will be any efficiency gain. http://www.mathworks.com/help/techdoc/ref/fscanf.html
I think the pythonic way to do it would be to open the file and read in the data into a
list
oflist
s using list comprehensions.(I'm using data from a string for clarity, and reading it in as if from a file using
StringIO
.)As an earlier answer mentions, numpy has a more direct method.
I think Wookai answer is incorrect. I think numpy.loadtxt is what you look for.
Python has no built-in
fscanf
function. The closest way to do it is to read the file line by line and use regular expressions.Numpy (the Matlab-like Python library), however, has a function that allows to read a file and construct an array from is content :
numpy.fromfile
(or, as suggested in the other answers,numpy.loadtxt
may be more suitable in this case).