在 Python 3 中导入 .dat 文件

2023-12-21

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

lines/header/numbers/lines 

像这个例子

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.

现在我试图读取这个文件,删除第一行和最后一行并将数字放入数组中并对它们进行一些基本计算,但我无法删除这些行。我用了data = np.genfromtxt('sample.dat')导入数据,但对于线条,我无能为力。谁能帮我?


也许这对您有帮助:

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)]

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Python 3 中导入 .dat 文件 的相关文章

随机推荐