簡體   English   中英

確保matlab的`fread`的python等價

[英]Ensuring python equivalence of matlab's `fread`

我在http://jmp.sh/VpTZxgQ找到了一個二進制測試文件,我試圖在python中重寫一些讀取此文件的matlab代碼。

我已經意識到,matlab的fread記住已經讀取的內容,以便它跳過已經讀取的字節數。 我如何確保在python中獲得相同的行為?

Matlab代碼:

clear all; close all;

path = pwd;
ext = 'bin';
stem = 'test';
filename = [stem,'.',ext];
filename = fullfile(path,filename);
fid = fopen(filename,'r');

fread(fid,2,'int16')
fread(fid,32,'char')
fread(fid,2,'int16')

Python代碼:

import numpy as np  

def fread(filename, n, precision):
     with open(filename, 'rb') as fid:
         data_array = np.fromfile(fid, precision).reshape((-1, 1)).T

     return data_array[0,0:n]

print fread('test.bin', 2, np.int16)                                                                                                                         
print fread('test.bin', 32, np.str)
print fread('test.bin', 2, np.int16) 

理想情況下,我希望這些配方的輸出相同,但它們不是。 事實上,當我嘗試將precision設置為np.str時,python會出現value error ...

作為一個額外的問題 - 我假設讀取二進制文件並理解數據需要用戶了解數據的格式,以便提供數據的任何合理信息。 這是真的?

正如評論所示,您需要使用文件描述符,這是Matlab代碼正在做的事情:

import numpy as np

def fread(fid, nelements, dtype):
     if dtype is np.str:
         dt = np.uint8  # WARNING: assuming 8-bit ASCII for np.str!
     else:
         dt = dtype

     data_array = np.fromfile(fid, dt, nelements)
     data_array.shape = (nelements, 1)

     return data_array

fid = open('test.bin', 'rb');

print fread(fid, 2, np.int16)
print fread(fid, 32, np.str)
print fread(fid, 2, np.int16)

以二進制文件讀取和寫入數據需要讀寫器就指定的格式達成一致。 正如評論者所說,如果您將二進制文件保存在一台計算機上並嘗試在另一台計算機上閱讀,那么endianess可能會成為一個問題。 如果始終在同一CPU上寫入和讀取數據,那么您將不會遇到此問題。

test.bin的輸出:

MATLAB Output             Python+Numpy Output
------------------------------------------------------
ans =                     

    32                    [[32]
     0                     [ 0]]

ans =                   

    35                    [[ 35]
    32                     [ 32]
    97                     [ 97]
   102                     [102]
    48                     [ 48]
    52                     [ 52]
    50                     [ 50]
    95                     [ 95]
    53                     [ 53]
    48                     [ 48]
   112                     [112]
   101                     [101]
   114                     [114]
    99                     [ 99]
    95                     [ 95]
   115                     [115]
   112                     [112]
    97                     [ 97]
   110                     [110]
    32                     [ 32]
    32                     [ 32]
    32                     [ 32]
    32                     [ 32]
    32                     [ 32]
    32                     [ 32]
    32                     [ 32]
    32                     [ 32]
    32                     [ 32]
    32                     [ 32]
    32                     [ 32]
    32                     [ 32]
    32                     [ 32]]

ans =

    32                     [[32]
     0                      [ 0]]

暫無
暫無

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

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