是否可以在Python中将二进制文件的内容作为字典读取?

时间:2018-10-31 07:53:51

标签: python matlab anaconda binaryfiles

我有二进制格式的原始数据文件。在我之前用MATLAB编写代码时,我想将其转换为PYTHON。

我已经附加了几个二进制文件。我的基于文件格式规范文档读取每个文件的MATLAB代码如下:


f1 = fopen('p580.004','r')
if (f1 ~= -1)
    must = fread(f1,1,'int16');
    internaltrigger = fread(f1,1,'int8'); 
    internaldwell = fread(f1,1,'int8'); 
    dwellunits = fread(f1,1,'int8'); 
    check_dwell(dwellunits==0)=2;
    check_dwell(dwellunits==1)=0.002;
    check_dwell(dwellunits==2)=0.000002;
    check_dwell(dwellunits==3)=200;
    check_dwell(dwellunits>3)=NaN;
    check_dwell(dwellunits<0)=NaN;
    acqmode = fread(f1,1,'int8'); 
    formatdwell = fread(f1,1,'int32'); 
    passlength = fread(f1,1,'int16');
    passcount = fread(f1,1,'int32');
    passpreset = fread(f1,1,'int32');
    starttime = fread(f1,8,'*char');
    startdate = fread(f1,8,'*char');
    markerchannel = fread(f1,1,'int16');
    mcsno = fread(f1,1,'int8');
    calibtype = fread(f1,1,'int8');
    calibunit = fread(f1,4,'*char');
    calibcoef0 = fread(f1,4,'int8');
    calibcoef1 = fread(f1,4,'int8');
    externaldwelthrevolt = fread(f1,4,'int8');
    reserv1 = fread(f1,1,'int32');
    reserv2 = fread(f1,1,'int8');
    replacethesum = fread(f1,1,'int8');
    identbyte = fread(f1,1,'*char');
    progdwethrevolt = fread(f1,1,'int8');
    detdeslength = fread(f1,1,'int8');
    detdes = fread(f1,63,'*char');
    samplength = fread(f1,1,'int8');
    sampledes = fread(f1,63,'*char');
    lindisflag = fread(f1,1,'int8');
    linscale = fread(f1,1,'int8');
    disstarchannel = fread(f1,2,'int8');
    disnumchannels = fread(f1,1,'int16');
    reser = fread(f1,10,'int8');
    discse = fread(f1,1,'int8');
    discedg = fread(f1,1,'int8');
    discvolt = fread(f1,4,'int8');
    scaupvolt =fread(f1,1,'float32');
    scalolvolt = fread(f1,1,'float32');
    dwelltime = fread(f1,1,'float32');
    setconsflag = fread(f1,1,'int8');
    reserve = fread(f1,13,'int8');
    calibcoef2 = fread(f1,1,'float32');
    calibcoef3 = fread(f1,1,'float32');
    mcsid = fread(f1,8,'*char');
    if (dwelltime == check_dwell) && (length(startdate)>=2)
        x1= fread(f1,1024,'int32'); % Photon Count
        if (isempty(x1) ~= 1) && (length(x1) == 1024) && (length(find(x1>0))>100)
            photoncount(1:1024) = x1;
        else
            photoncount(1:1024) = NaN;
        end
        %%%%%%% converting char array to string %%%%%
        startdate=cell2mat(cellstr(startdate)');
        starttime=cell2mat(cellstr(starttime)');
        %%%%%%% checking for date format %%%%%
        test=find(startdate == '-');
        if (isempty(test) ~=1)
            day2=startdate(1,4:5);
            month2=startdate(1,1:2);
            year2=pfiles1(i).date(8:11);
        else
            if (strcmp(startdate,'') ~=1)
                day2=startdate(1,3:4);
                month2=startdate(1,1:2);
                year2=startdate(1,5:8);
            else
                day2='';
                month2='';
                year2='';
            end
        end
end

在PYTHON中,我编写了以下代码:

from array import array

fname = 'p580.004'

with open(fname,'rb') as file:

      pdata = array( 'B', file.read() )  # buffer the file

pdata输出结果如下:

pdata = 

Out[1]: array('B', [252, 255, 1, 0, 0, 1, 2, 0, 0, 0, 0, 4, 212, 48, 0, 0, 212, 48, 0, 0, 48, 48, 58, 49, 52, 58, 51, 54, 48, 57, 50, 57, 50, 48, 49, 56, 75, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 70, 179, 63, 0, 0, 0, 0, 0, 1, 170, 1, 7, 77, 67, 66, 32, 49, 50, 49, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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, 0, 2, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 4, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 4, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 4, 0, 0, 0, 1, 0, 0, 0, 3, 0, 0, 0])

如果我使用以下Python代码:

with open(fname,'rb') as f:  # b is important -> binary
    fileContent = f.read()

现在,fileContent输出以十六进制格式给出输出。

以下是我的查询:

  1. 有没有一种方法可以将二进制文件作为Python中的字典读取?
  2. 如您所见,您将看到MATLAB代码,其中包含不同格式的变量,例如int8,int16,int32,char,float32。如何用我编写的Python代码处理这些不同的格式?
  3. 我尝试搜索Internet,但不了解如何识别二进制文件中的数据格式(假设您没有格式规范信息)以及如何为变量分配实际值。
  4. 有人可以帮助我将MATLAB脚本转换为Python代码以读取二进制文件吗?

根据建议,我将代码修改如下:

fname = 'p580.004'
with open(fname, 'rb') as fd:
    binbuf = fd.read()

data_names = ('must','internaltrigger','internaldwell','dwellunits', \
              'acqmode','formatdwell','passlength','passcount','passpreset', \
              'starttime','startdate','markerchannel','mcsno','calibtype', \
              'calibunit','calibcoef0','calibcoef1','externaldwelthrevolt', \
              'reserv1','reserv2','replacethesum','identbyte', \
              'progdwethrevolt','detdeslength','detdes','samplength', \
              'sampledes','lindisflag','linscale','disstarchannel', \
              'disnumchannels','reser','discse','discedg','discvolt', \
              'scaupvolt','scalolvolt','dwelltime','setconstflag', \
              'reserve','calibcoef2','calibcoef3','mcsid','Ncount')
data_tuple = struct.unpack_from('hbbbbihii8s8schbb4s4b4b4bibbcbb63pb63cbb2bh10bbb4bfffh13hff8c1024i', binbuf)

我面临的主要问题是定义二进制文件的格式。 len(binbuf)和struct.calcsize('hbbbbihii8s8schbb4s4b4b4bibbcbb63pb63cbb2bh10bbb4bfffh13hff8c1024i')不一致。有人可以根据上面指出的MATLAB代码建议我格式化的地方出问题了吗?

2 个答案:

答案 0 :(得分:1)

要在Python3中将二进制数据读入变量,有一个struct模块。它允许指定如何解释字节(例如,无符号的长整数,带符号的字符等)以及格式字符串的顺序。

例如在以下示例中,对struct.unpack_from()的调用中的格式字符串“ hbb”表示:

  • 'h':读取一个16位带符号的int
  • 'b':然后是8位整数
  • 'b':然后是另一个8位int

从二进制文件读取前三个变量的简单示例:

import struct

fname = 'p580.004'
with open(fname, 'rb') as fd:
    binbuf = fd.read()

must, internaltrigger, internaldwell = struct.unpack('hbb', binbuf)
print('must:', must)
print('internaltrigger:', internaltrigger)
print('internaldwell:', internaldwell)

或者,按照您的要求将其读入词典:

import struct

fname = 'p580.004'
with open(fname, 'rb') as fd:
    binbuf = fd.read()

data_names = ('must', 'internaltrigger', 'internaldwell' )
data_tuple = struct.unpack('hbb', binbuf)
dictionary = dict(zip(data_names, data_tuple))
print(dictionary)

有关此的更多信息:https://docs.python.org/3/library/struct.html

答案 1 :(得分:0)

我可以使用numpy编写PYTHON代码,如下所示:

import numpy as np

dt=np.dtype([('must',np.int16),
   ('nternaltrigger',np.int8),
   ('internaldwell',np.int8),
   ('dwellunits',np.int8),
   ('acqmode',np.int8),
   ('formatdwell',np.int32),
   ('passlength',np.int16),
   ('passcount',np.int32),
   ('passpreset',np.int32),
   ('starttime','|S8'),
   ('startdate','|S8'),
   ('markerchannel',np.int16),
   ('mcsno',np.int8),
   ('calibtype',np.int8),
   ('calibunit','|S4'),
   ('calibcoef0','|S4'),
   ('calibcoef1','|S4'),
   ('externaldwelthrevolt',np.int8,(4,)),
   ('reserv1',np.int32),
   ('reserv2',np.int8),
   ('replacethesum',np.int8),
   ('identbyte','|S1'),
   ('progdwethrevolt',np.int8),
   ('detdeslength',np.int8),
   ('detdes','|S63'),
   ('samplength',np.int8),
   ('sampledes','|S63'),
   ('lindisflag',np.int8),
   ('linscale',np.int8),
   ('disstarchannel',np.int8,(2,)),
   ('disnumchannels',np.int16),
   ('reser',np.int8,(10,)),
   ('discse',np.int8),
   ('discedg',np.int8),
   ('discvolt',np.int8,(4,)),
   ('scaupvolt',np.float32),
   ('scalolvolt',np.float32),
   ('dwelltime',np.float32),
   ('setconsflag',np.int8),
   ('reserve',np.int8,(13,)),
   ('calibcoef2',np.float32),
   ('calibcoef3',np.float32),
   ('mcsid','|S8'),
   ('photocount',np.int32,(1024,))])

f=np.fromfile('p580.004',dtype=dt)
d=dict(zip(f.dtype.names,f[0]))

这可以按要求正常工作。感谢大家的建议和初步帮助。