猿问

python中二进制文件的非连续数据读取

我有一个二进制文件,其中包含来自传感器的时间序列。数据格式如下:


#1(t0)#2(t0)#3(t0)...#n(t0)#1(t1)#2(t1)#3(t1)...#n(t1)...


一次,来自n个传感器的测量数据以二进制格式存储在文件中。我想重建传感器的时间序列,以便


#1(t0)#1(t1)#1(t2)...


从#1(t0)到#1(t1)的距离,步幅是固定的并且是已知的,并且传感器的数量也是已知的。以下代码是我的实现。我的实现方式是尝试一次获取单个数据,而不是那么快。是否有任何方法可以像MPI中的集体io中那样提高读取非连续数据的速度?


def collect_signal(fp, channel_no, stride, dtype):

    byteSize = np.dtype(dtype).itemsize

    fp.seek(0,2) # go to the file end

    eof = fp.tell() # get the eof address

    fp.seek(0,0) # rewind


    fp.seek(0 + channel_no,0) # starting point per each channel

    signal = []

    while True:

        start = fp.tell()

        sample = np.frombuffer(fp.read(byteSize), dtype=dtype)

        signal.append(sample[0])

        if fp.tell() == eof or fp.tell() + stride > eof:

            break;

        else:

            fp.seek(start + stride, 0)


    return signal


小唯快跑啊
浏览 174回答 2
2回答
随时随地看视频慕课网APP

相关分类

Python
我要回答