What is the equivalent of Matlab 'fscanf'

2020-07-18 05:16发布

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条回答
相关推荐>>
2楼-- · 2020-07-18 06:00

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.

查看更多
不美不萌又怎样
3楼-- · 2020-07-18 06:00

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

查看更多
趁早两清
4楼-- · 2020-07-18 06:09

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.

查看更多
Deceive 欺骗
5楼-- · 2020-07-18 06:10

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

查看更多
走好不送
6楼-- · 2020-07-18 06:14

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

查看更多
登录 后发表回答