所以我刚刚开始了 python 编程类(class),我有一个名为“风数据分析”的作业,其中我要从 bin.file 中提取数据并将其排序为 x、y 和 z 值。到目前为止我得到了:
filename="turb21351_L72u.bin"
with open(filename,'br') as f:
buffer = f.read(100000)
print("Length of buffer is %d" % len(buffer))
for i in buffer:
print(int(i))
效果很好(注意;我在这里编写的脚本存在一些缩进错误),并且给我的值范围为 1 到 300。
问题在于对数据进行排序。作业的描述听起来像这样:
“数据文件由Nz X Ny X Nx数字(浮点单精度)组成。数字的顺序对应于索引 z、y和x分别从1依次增加到Nz、Ny和Nx。变化最快的索引是 z,其次是 y,变化最慢的索引是 x。那是, 序列中的前 Nz 数字对应于从 1 到 Nz、y = 1 和 x = 1 的索引 z。根据此排序规则,该函数必须将数据转换为维度为 Nz X Ny X 的三维数组Nx。”
我的问题是:
如何从数学角度理解作业描述以及如何根据排序规则对其进行排序?
最佳答案
您可以尝试使用以下代码吗:
filename="turb21351_L72u.bin"
with open(filename,'br') as f:
buffer = f.read(100000)
print("Length of buffer is %d" % len(buffer))
for i in buffer:
print(int(i))
在 python 中,在处理非托管资源(如文件流)时使用 with
关键字。 with
语句的工作方式类似于 block 语句,但需要缩进。
来自Python Docs :
The
with
statement clarifies code that previously would usetry...finally
blocks to ensure that clean-up code is executed. In this section, I’ll discuss the statement as it will commonly be used. In the next section, I’ll examine the implementation details and show how to write objects for use with this statement.The
with
statement is a control-flow structure whose basic structure is:with expression [as variable]: with-block
The expression is evaluated, and it should result in an object that supports the context management protocol (that is, has
__enter__()
and__exit__()
methods).
关于python - 从二进制文件中提取数据并对其进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44603135/