5

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]); 

5 Answers 5

5

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).

Sign up to request clarification or add additional context in comments.

2 Comments

numpy.fromfile is very low-level. If you want to read in binary data or a very simply formatted text file very, very quickly, then it's a good option. Generally speaking, though, the OP would probably be better off with numpy.loadtxt or numpy.genfromtxt for this purpose. fromfile is better suited binary data, though it can handle simple text formats as well. loadtxt and genfromtxt are much, much more flexible, but slightly slower. (The difference between the two is that genfromtxt handles missing values while loadtxt can't)
Thanks, I updated my answer in case the OP did not read your insightful comment.
3

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.

Comments

1

I think Wookai answer is incorrect. I think numpy.loadtxt is what you look for.

Comments

0

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

Comments

0

I think the pythonic way to do it would be to open the file and read in the data into a list of lists 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.