簡體   English   中英

MATLAB 的 fread 與 Python 中的 skip 參數等效嗎?

[英]What is equivalent of MATLAB's fread with skip parameter in Python?

我想知道如何實現 MATLAB

fread(fileID,sizeA,precision,skip)

在 Python(文檔)中。 有很多建議如何處理它以防萬一

fread(fileID,sizeA,precision)

但我需要skip參數。 所以我想獲得一些

def fread(fileID,sizeA,precision,skip):
    # some code which do the same thing as matlab fread(fileID,sizeA,precision,skip)
    pass

沒有符號讀取如何實現?

您可以使用 Python 的struct 模塊來解析復雜的二進制結構,包括填充字節。 例如,復制 Matlab 文檔,如果要讀取 2 個短整數后跟 2 個填充字節的文件:

import struct
fmt = "=hhxx" #native endianness and no alignment (=), two shorts (h), two pad bytes (x)
data = [x for x in struct.iter_unpack(fmt, open("nine.bin", "rb").read())]
## [(1, 2), (4, 5), (7, 8)]

請注意,struct.iter_unpack 的struct.iter_unpack和其他解包方法是一個元組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM