python - 如何使用 python 代码从二进制文件中读取和提取值?
问题描述
我对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 个参数 - radius 和 phi。radius 和 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)
附录:当谈到不鼓励使用and时,numpy 文档非常明确:np.tofile()
np.fromfile()
不要依赖 tofile 和 fromfile 的组合来存储数据,因为生成的二进制文件不是平台独立的。特别是,不保存字节顺序或数据类型信息。数据可以使用 save and load 以独立于平台的 .npy 格式存储。
个人旁注:如果你花了几天时间来理解这段代码,不要因为学习而气馁python
;我们都从某个地方开始。我建议您诚实地对待您的教授遇到的障碍(如果这出现在谈话中),因为她/他应该能够在编程时正确断言“您在哪里”。:-)
推荐阅读
- php - 标头之前的脚本输出结束(PHP 和 Apache)
- batch-file - 有没有办法提取 TXT 文件并将其转换为批处理文件?
- mysql - MySQL 解析和拆分 JSON 值
- mongodb - 从 Mongo DB 中的文档中检索值
- javascript - 如何在反应组件中添加和删除类名?
- javascript - 对于javascript中的char打高尔夫球,使用编码进行编码,使用另一个进行解码
- r - 根据 R 中的行名操作列值
- android - 如何解决 Android 应用中的 Google 身份验证问题?
- python - 包含数字但不包含其他数字的正则表达式
- r - 如何替换 R 中日期对象中的日期?