简体   繁体   English

读取具有固定列数但行数未知的二维数组的二进制文件的最快方法

[英]Fastest way to read binary file with 2d array with fixed number of columns but unknown rows

I have a binary files that has saved values of a 2d array. 我有一个已保存2d数组值的二进制文件。 All values are saved in double format (8bytes) The data is written to the file row by row. 所有值均以双格式(8字节)保存。数据被逐行写入文件。 I want to read the file as fast as possible without knowing how many rows the file has. 我想在不知道文件有多少行的情况下尽快读取文件。

I am doing it this way, but I was wondering if there is a faster method than this: 我这样做,但是我想知道是否有比这更快的方法:

with open("myfile", "rb") as f:
    byte = f.read(8)
    while byte != "":
        # Do stuff with byte.
        byte = f.read(8)
with open("myfile", "rb") as f:
    for i in f:
        #i is now your line, this only gathers it once.

btw your code is faulty the reasong your asking for it to be faster is because you stuck your self in a infinity loop when the line is empty you will get " "*8 not "" because you asked it to read the first 8 顺便说一句,您的代码有错误,您要求更快的原因是因为当行为空时,您将自己陷入无限循环中,您将得到“ * 8 not”,因为您要求它读取前8个

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

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