从Python中的文件中提取所需的字节
我有一个二进制文件在这里:
ftp://n5eil01u.ecs.nsidc.org/SAN/GLAS/GLA06.034/2003.02.21/GLA06_634_1102_001_0079_3_01_0001.DAT
I have a binary file here: ftp://n5eil01u.ecs.nsidc.org/SAN/GLAS/GLA06.034/2003.02.21/GLA06_634_1102_001_0079_3_01_0001.DAT
我必须从该文件中提取以下数据:
I have to extract the following data from that file:
Byte Offset: 176
Data type: 4-byte (long) integer
Total bytes: 160
我试过如下:
import numpy as np
fname = 'GLA06_634_1102_001_0079_3_01_0001.DAT'
with open(fname,'rb') as fi:
fi.seek (176,0)
data= np.fromfile(fi,dtype='long',count=160)
print data
没有成功,有什么不对我的想法?
No success, what's wrong with my idea?
使用的硬codeD 的偏移量是相当的脆弱的解决方案。不过,假设你知道你在做什么:
Using a hard coded offset is a rather fragile solution. But assuming you know what you are doing:
Byte Offset: 176
Data type: 4-byte (long) integer
Total bytes: 160
AKAICT,从而导致160/4 = 40 值读取(你能证实吗?)
AKAICT, that leads to 160/4 = 40 values to read (could you confirm that?)
在另外的类型应该是numpy的定义类型中的一个。在这里, np.int32
可能是正确的:
In addition, the type should be one of the numpy defined type. Here np.int32
might be the right one:
data= np.fromfile(fi,dtype=np.int32,count=40)
在我的电脑上,这将产生以下结果:
On my computer, this produces the following result:
[1919251297 997485633 1634494218 1936678771 1634885475 825124212
808333629 808464432 942813232 1818692155 1868526433 1918854003
1600484449 1702125924 842871086 758329392 841822768 1728723760
1601397100 1600353135 1702125938 1835627615 1026633317 809119792
808466992 1668483643 1668509535 1952543327 1026633317 960048688
960051513 909654073 926037812 1668483643 1668509535 1952543327
1633967973 825124212 808464957 842018099]
如果这不是什么预期,也许你有字节序的问题。
numpy的为自定义类型来解决这个问题:
Numpy as support for custom defined types to solve that problem:
例如:
-
np.dtype('< I4')
为4个字节(符号)整数的小尾数的 -
np.dtype('> I4')
为4个字节(符号)整数的大端的
-
np.dtype('<i4')
is 4 bytes (signed) integer little endian -
np.dtype('>i4')
is 4 bytes (signed) integer big endian
在你的情况下,强制读取数据作为小字节序,你可能会这样写:
In you case, to force reading data as little endian, you might write:
dt = np.dtype('<i4')
with open(fname,'rb') as fi:
fi.seek (176,0)
data= np.fromfile(fi,dtype=dt,count=40)
print data