您需要以二进制模式打开文件。
with open('JAD_L30_LRS_ELC_ANY_CNT_2018091_V03.DAT', 'rb') as f:
while True:
chunk = f.read(160036) # that is record size as per LBL file
# because the file is huge it will expect to hit Enter
# to display next chunk. Use Ctrl+C to interrupt
print(chunk)
input('Hit Enter...')
注意,您可以解析 LBL 文件,构造格式字符串以与 struct 模块一起使用,并将每个块解析为有意义的字段。这就是您引用的评论的意思。
"""Example of reading NASA JUNO JADE CALIBRATED SCIENCE DATA
https://pds-ppi.igpp.ucla.edu/search/view/?f=yes&id=pds://PPI/JNO-J_SW-JAD-3-CALIBRATED-V1.0/DATA/2018/2018091/ELECTRONS/JAD_L30_LRS_ELC_ANY_CNT_2018091_V03&o=1
https://stackoverflow.com/a/66687113/4046632
"""
import struct
from functools import reduce
from operator import mul
from collections import namedtuple
__author__ = "Boyan Kolev, https://stackoverflow.com/users/4046632/buran"
with open('JAD_L30_LRS_ELC_ANY_CNT_2018091_V03.LBL') as f:
rjws = [line.strip('\n/* ') for line in f if line.startswith('/* RJW')]
# create the format string for struct
rjws = rjws[2:] # exclude first 2 RJW comments related to file itself
names = []
FMT = '='
print(f'Number of objects: {len(rjws)}')
for idx, rjw in enumerate(rjws):
_, name, fmt, num_dim, *dims = rjw.split(', ')
fstr = f'{reduce(mul, map(int, dims))}{fmt}'
FMT = f'{FMT} {fstr}'
names.append(name)
print(f'{idx}:{name}, {fstr}')
FMT = FMT.replace('c', 's') # for conveninece treat 21c as s char[]
print(f"Format string: {repr(FMT)}")
# parse DAT file
s = struct.Struct(FMT)
print(f'Struct size:{s.size}')
with open('JAD_L30_LRS_ELC_ANY_CNT_2018091_V03.DAT', 'rb') as f:
n = 0
while True: # in python3.8+ this loop can be simplified with walrus operator
chunk = f.read(s.size)
if not chunk:
break
data = s.unpack_from(chunk)
# process data further, e.g. split data in 2D containers where appropriate
n += 1
print(f'Number of records: {n}')
# make a named tuple to represent first 10 fields
# for nice display. This basic use of namedtuple works only
# for first 23 objects, which have single item.
num_fields = 10
Record = namedtuple('Record', names[:num_fields])
record = Record(*data[:num_fields])
print('\n----------------------\n')
print(f'First {num_fields} fields of the last record.')
print(record)
输出:
Number of objects: 49
0:DIM0_UTC, 21c
1:PACKETID, 1B
2:DIM0_UTC_UPPER, 21c
--- omitted for sake of brevity ---
46:DIM2_AZIMUTH_DESPUN_LOWER, 3072f
47:MAG_VECTOR, 3f
48:ESENSOR, 1H
Format string: '= 21s 1B 21s 1b 21s 1b 1H 1B 1B 1B 1B 1h 1h 1f 1f 1f 1f 1f 1f 1f 1f 1f 1f 3f 3f 3f 1f 9f 9f 9f 1f 1I 1I 1H 3072f 3072f 3072f 3072f 3072f 3072f 3072f 3072f 3072f 3072f 3072f 3072f 3072f 3f 1H'
Struct size:160036
Number of records: 1101
----------------------
First 10 fields of the last record.
Record(DIM0_UTC=b'2018-091T23:56:08.925', PACKETID=106, DIM0_UTC_UPPER=b'2018-092T00:01:08.925', PACKET_MODE=1, DIM0_UTC_LOWER=b'2018-091T23:51:08.925', PACKET_SPECIES=-1, ACCUMULATION_TIME=600, DATA_UNITS=2, SOURCE_BACKGROUND=3, SOURCE_DEAD_TIME=0)
链接到GutHub gist