我有一个巨大的文件要加载,所以我不能直接打开它。我想我必须分多个部分阅读它。
例如,为了使用第 1 到 50 行,我尝试了类似的方法,但它不起作用:
import numpy as np
with open('test.txt') as f:
lines = (line for line in f if line < 50.)
FH = np.loadtxt(lines, delimiter=',', skiprows=1)
请您参考如下方法:
当您使用 numpy 时,您可以使用 islice()内置 itertools 库中的函数仅加载前 50 行,如下所示:
import numpy as np
import itertools
with open('test.txt') as f_input:
FH = np.loadtxt(itertools.islice(f_input, 0, 50), delimiter=',', skiprows=1)
