我有二进制格式的原始数据文件。虽然我之前已经在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
在巨蟒中,我写了以下代码:
from array import array
fname = 'p580.004'
with open(fname,'rb') as file:
pdata = array( 'B', file.read() ) # buffer the file
updatea输出结果如下:
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, 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, 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输出以十六进制格式给出输出。
以下是我的疑问:
- 有没有一种方法可以将二进制文件作为Python中的Dictionary读取
- 正如您在MATLAB代码中看到的,有不同格式的变量,例如,int8、int16、int32、char、float32。如何用我编写的Python代码处理这些不同的格式
- 我试过在互联网上搜索,但不知道如何识别二进制文件中数据的格式(假设你没有格式规范信息(以及如何将实际值分配给变量
- 有人能帮我把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('hbbbihii8s8schbb4s4b4bibbcbb63pb63cbb2bh10bb4bfffh13hff8c1024i'(不一致。有人能根据上面指出的MATLAB代码向我建议我在格式上哪里出了问题吗?
要将二进制数据读取到python3中的变量中,有struct
模块。它允许指定如何解释字节(例如,无符号长、有符号字符等(以及按格式字符串的顺序。
例如,以下示例中对struct.unpack_from()
的调用中的格式字符串"hbb"表示:
- 'h':读取一个16位带符号的int
- "b":然后是8位int
- "b":然后是另一个8位int
从二进制文件中读取前3个变量的快速示例:
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
我可以使用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]))
这符合要求。感谢大家的建议和初步帮助。