在 Python 3 中导入 .dat 文件

Fay*_*aye 7 python numpy

我想导入一个.dat文件,其中包括

lines/header/numbers/lines 
Run Code Online (Sandbox Code Playgroud)

像这个例子

start using data to calculate something
 x y z g h 
 1 4 6 8 3
 4 5 6 8 9 
 2 3 6 8 5
end the data that I should import.
Run Code Online (Sandbox Code Playgroud)

现在我正在尝试读取此文件,删除第一行和最后一行并将数字放入数组中并对其进行一些基本计算,但我无法摆脱这些行。我曾经data = np.genfromtxt('sample.dat')导入数据,但是有了线,我什么也做不了。谁能帮我?

Rom*_*eev 8

也许这对你有帮助:

import numpy as np

data = np.genfromtxt('sample.dat',
                     skip_header=1,
                     skip_footer=1,
                     names=True,
                     dtype=None,
                     delimiter=' ')
print(data)
# Output: [(1, 4, 6, 8, 3) (4, 5, 6, 8, 9) (2, 3, 6, 8, 5)]
Run Code Online (Sandbox Code Playgroud)

有关所用参数的更多信息,请参阅 numpy 文档:https : //numpy.org/doc/stable/reference/generated/numpy.genfromtxt.html