Python中的Matlab'fscanf'等效项是什么?

编程入门 行业动态 更新时间:2024-10-24 21:35:56
本文介绍了Python中的Matlab'fscanf'等效项是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

Matlab函数fscanf()似乎非常强大. python(或numpy)中是否有相同的东西?

The Matlab function fscanf() seems to be very powerful. Is there any equivalent of the same in python (or numpy)?

具体来说,我想从文件中读取矩阵,但是我不想遍历每一行以读取矩阵. 这种东西(从matlab读取2D 1000x1000矩阵):

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

推荐答案

Python没有内置的fscanf函数.最接近的方法是逐行读取文件并使用正则表达式.

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 (类似于Matlab的Python库)具有允许读取文件的功能.并从is内容构造一个数组: numpy.fromfile (或根据其他答案的建议, numpy.loadtxt 在这种情况下可能更合适.)

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

更多推荐

Python中的Matlab'fscanf'等效项是什么?

本文发布于:2023-10-07 15:32:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1469786.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Matlab   Python   fscanf

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!