python - 在 Python 3 中导入 .dat 文件

标签 python numpy

我想导入一个 .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

关于python - 在 Python 3 中导入 .dat 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42704087/

相关文章:

python - 创建或更新Python字典条目

javascript - 带有 XHR 对象的 GET 请求,未找到 URI 组件名称

python - 随机选择非唯一最大值的argmax

python - 为 panda 切片添加值的有效方法

python - 子类化 numpy.ma.MaskedArray

python - 从文件夹中的图像序列获取 numpy 数组

python - 在 elementtree 中使用 SimpleXMLTreeBuilder

python - 如何使用 Python 中的 Perl 模块?

python - 在 Python 中复制对象实例

python - 成功安装 NumPy,但无法使用 virtualenv 访问