Andy Stow Away Andy Stow Away - 4 years ago 389
Python Question

Skip rows when reading data from file using numpy.genfromtxt

I have a data set in a file as shown below

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

I would like to extract only rows which contain
. How do I do that with
? Is there any other function available in numpy that helps me do the same?

Answer Source

numpy.genfromtxt accepts generators, so you can chain genfromtext and ifilter:

from itertools import ifilter
with open(fname, 'rb') as inp:
     filtered_inp = ifilter(lambda x: x.startswith('A'), inp)
     a = np.genfromtxt(filtered_inp)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download