Reputation: 3521
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]);
Upvotes: 5
Views: 11092
Reputation: 21723
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).
Upvotes: 5
Reputation: 26586
I think the pythonic way to do it would be to open the file and read in the data into a list
of list
s using list comprehensions.
(I'm using data from a string for clarity, and reading it in as if from a file using StringIO
.)
>>> from cStringIO import StringIO
>>> data_file="1 2 3 4 5 6\n7 8 9 10 11 12\n13 14 15 16 17 18\n19 20 21 22 23 24\n"
>>> reader=StringIO(data_file)
>>> array=[map(int, reader.readline().split()) for i in xrange(4)]
>>> array
[[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 17, 18], [19, 20, 21, 22, 23, 24]]
As an earlier answer mentions, numpy has a more direct method.
Upvotes: 0
Reputation: 11195
I think Wookai answer is incorrect. I think numpy.loadtxt is what you look for.
Upvotes: 1
Reputation: 3846
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
Upvotes: 0
Reputation: 6957
I'm pretty sure there is not, but iterating isn't too hard. This would do it:
matrix = []
for i in open('input.txt'):
matrix.append( map(int, i.split()) )
If you need something more complex (i.e. not just ints separated by single characters), regular expressions may be the way to go.
Upvotes: 3