繁体   English   中英

如何使用python代码从二进制文件中读取和提取值?

[英]How to read and extract values from a binary file using python code?

我对python比较陌生。 作为我的天文学项目工作的一部分,我必须处理二进制文件(当然这对我来说也是新的)。 我得到了一个二进制文件和一个从二进制文件中读取数据的python代码。 然后我的教授要求我理解代码如何在二进制文件上运行。 我花了几天时间想弄明白,但没有任何帮助。 这里的任何人都可以帮我代码吗?

# Read the binary opacity file
f = open(file, "r")

# read file dimension sizes
a = np.fromfile(f, dtype=np.int32, count=16)
NX, NY, NZ = a[1], a[4], a[7]


# read the time and time step
time, time_step = np.fromfile(f, dtype=np.float64, count=2)

# number of iterations
nite = np.fromfile(f, dtype=np.int32, count=1)

# radius array
trash = np.fromfile(f, dtype=np.float64, count=1)
rad = np.fromfile(f, dtype=np.float64, count=a[1])

# phi array
trash = np.fromfile(f, dtype=np.float64, count=1)
phi = np.fromfile(f, dtype=np.float64, count=a[4])

# close the file
f.close()

据我所知,二进制文件包含几个参数(例如:半径,phi,声速,辐射能量)及其多个值。 上面的代码从二进制文件中提取值2 parameters- radius和phi。 半径和phi都有超过100个值。 该程序有效,但我无法理解它是如何工作的。 任何帮助,将不胜感激。

二进制文件基本上只是一长串连续数据; 你需要告诉np.fromfile()在哪里查看以及期望什么类型的数据 如果你创建自己的文件,也许最容易理解:

import numpy as np

with open('numpy_testfile', 'w+') as f:
    ## we create a "header" line, which collects the lengths of all relevant arrays
    ## you can then use this header line to tell np.fromfile() *how long* the arrays are
    dimensions=np.array([0,10,0,0,10,0,3,10],dtype=np.int32)
    dimensions.tofile(f) ## write to file

    a=np.arange(0,10,1) ## some fake data, length 10
    a.tofile(f) ## write to file
    print(a.dtype)

    b=np.arange(30,40,1) ## more fake data, length 10
    b.tofile(f) ## write to file
    print(b.dtype)

    ##  more interesting data, this time it's of type float, length 3
    c=np.array([3.14,4.22,55.0],dtype=np.float64) 
    c.tofile(f) ## write to file
    print(c.dtype)

    a.tofile(f) ## just for fun, let's write "a" again

with open('numpy_testfile', 'r+b') as f:
    ### what's important to know about this step is that 
    #   numpy is "seeking" the file automatically, i.e. it is considering 
    #   the first count=8, than the next count=10, and so on 
    #   as "continuous data"
    dim=np.fromfile(f,dtype=np.int32,count=8)
    print(dim) ## our header line: [ 0 10  0  0 10  0  3 10]
    a=np.fromfile(f,dtype=np.int64,count=dim[1])## read the dim[1]=10 numbers
    b=np.fromfile(f,dtype=np.int64,count=dim[4])## and the next 10
    ## now it's dim[6]=3, and the dtype is float 10
    c=np.fromfile(f,dtype=np.float64,count=dim[6] )#count=30)
    ## read "the rest", unspecified length, let's hope it's all int64 actually!
    d=np.fromfile(f,dtype=np.int64) 

print(a)
print(b)
print(c)
print(d)

附录:阻止使用 np.tofile()np.fromfile()时, numpy文档是非常明确的:

不要依赖tofile和fromfile的组合来进行数据存储,因为生成的二进制文件不是独立于平台的。 特别是,不保存字节顺序或数据类型信息。 数据可以使用保存和加载以独立于平台的.npy格式存储。

个人注意事项:如果你花了几天时间来理解这段代码,不要因为学习python而气馁; 我们都从某个地方开始。 我建议诚实地告诉你教授遇到的障碍(如果出现在谈话中),因为她/他应该能够在编程时正确地断言“你在哪里”。 :-)

from astropy.io import ascii    
data = ascii.read('/directory/filename')
column1data = data[nameofcolumn1]
column2data = data[nameofcolumn2]

等。 column1data现在是该标题下所有值的数组我使用此方法导入ASCII格式的SourceExtractor数据文件。 我相信这是一种从ascii文件导入数据的更优雅方式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM