gpt4 book ai didi

python - 预先获取标题行数

转载 作者:行者123 更新时间:2023-11-28 21:19:03 25 4
gpt4 key购买 nike

我有一个由标题行和二进制部分组成的二进制文件。 ftp://n5eil01u.ecs.nsidc.org/SAN/GLAS/GLA06.034/2003.02.21/GLA06_634_1102_001_0079_3_01_0001.DAT

我必须知道标题行占用的行数。我怎么能事先知道它,以便我可以将值放在下面以转义 header 部分。

import numpy as np    
fname = 'GLA06_634_1102_001_0079_3_01_0001.DAT'

with open(fname,'rb') as fi:
fi.seek (176,0) ##HERE I HAVE TO PUT

最佳答案

来自提供的read file routine :

n_headers = long( read_header( i_file, 'NUMHEAD', error=error) )
recl= long( read_header( i_file, 'RECL', error=error) )
offset=long(recl*n_headers)
print,'offset=',offset
print,'recl n_headers = ',recl,n_headers
str_vers = 'pv'+strtrim(string(ver1),2)+'_'+ $
strtrim(string(ver2),2)
print, 'version=',str_vers

header 大小似乎是 recl*n_headers,其中这两个值是前两个 header 。所以:

fname = 'GLA06_634_1102_001_0079_3_01_0001.DAT'

with open(fname,'rb') as fi:
recl = None
numhead = None

# Loop in case the required headers are not the first two one
# and/or in wrong order
for line in fi:
if line.startswith('Recl='):
recl = int(line[5:-2])
if line.startswith('Numhead='):
numhead = int(line[8:-2])

if recl is not None and numhead is not None:
break

offset = recl*numhead

print "Binary data starts at ", offset
fi.seek(offset)

关于python - 预先获取标题行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25429233/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com