python fromfile 读取多个文件_使用读取二进制文件np.fromfile文件()
我有一个二进制文件,有许多部分。每个部分都有自己的模式(即整数、浮点和字符串的位置)。在
每个部分的模式是已知的。但是,该模式在部分中出现的次数是未知的。每个记录都在两个相同的整数之间。这些整数表示记录的大小。节名称位于两个整数记录长度变量之间:8和8。在每个部分中,还有多个记录(已知)。在Header
---------------------
Known header pattern
---------------------
8 Section One 8
---------------------
Section One pattern repeating i times
---------------------
8 Section Two 8
---------------------
Section Two pattern repeating j times
---------------------
8 Section Three 8
---------------------
Section Three pattern repeating k times
---------------------
我的方法是:Loop through and read each record using f.read(record_length), if the record is 8 bytes, convert to string, this will be the section name.
Then i call: np.fromfile(file,dtype=section_pattern,count=n)
我给每个部分打np.fromfile。在
我面临的问题有两个方面:How do I determine n for each section without doing a first pass read?
Reading each record to find a section name seems rather inefficient. Is there a more efficient way to accomplish this?
节名总是在两个整数记录变量之间:8和8。在
下面是一个示例代码,请注意,在这种情况下,我不必指定count,因为OES部分是最后一个部分:
^{pr2}$