最近有人询问如何在python中执行文件slurp,并且接受的答案提示如下:
with open('x.txt') as x: f = x.read()
Run Code Online (Sandbox Code Playgroud)
我将如何执行此操作来读取文件并转换数据的字节序表示?
例如,我有一个1GB的二进制文件,它只是一堆单精度浮点数打包为大端,我想将它转换为小端并转储到一个numpy数组.下面是我为完成此操作而编写的函数以及一些调用它的实际代码.我使用struct.unpackendian转换并试图通过使用来加速一切mmap.
那么我的问题是,我在正确使用啜食与mmap和struct.unpack?有更清洁,更快的方法吗?现在我的作品,但我真的想学习如何更好地做到这一点.
提前致谢!
#!/usr/bin/python
from struct import unpack
import mmap
import numpy as np
def mmapChannel(arrayName, fileName, channelNo, line_count, sample_count):
"""
We need to read in the asf internal file and convert it into a numpy array.
It is stored as a single row, and is binary. Thenumber of lines (rows), samples (columns),
and channels all come from the .meta text …Run Code Online (Sandbox Code Playgroud)