gpt4 book ai didi

python - 使用 numpy.genfromtxt 从文件读取数据时跳过行

转载 作者:太空宇宙 更新时间:2023-11-03 12:40:06 26 4
gpt4 key购买 nike

我在文件中有一个数据集,如下所示

A 1
B 2
C 3
A 4
B 5
C 6

我只想提取包含 'A' 的行。我如何使用 numpy.genfromtxt 做到这一点? numpy 中是否有任何其他功能可以帮助我做同样的事情?

最佳答案

numpy.genfromtxt 接受生成器,因此您可以链接 genfromtextifilter:

from itertools import ifilter
with open(fname, 'rb') as inp:
filtered_inp = ifilter(lambda x: x.startswith('A'), inp)
a = np.genfromtxt(filtered_inp)

关于python - 使用 numpy.genfromtxt 从文件读取数据时跳过行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20029971/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com