0

I am looking for the numpy equivalent of Matlab's

M = dlmread(filename,delimiter,[R1 C1 R2 C2])

In numpy's loadtxt, I found that you can skip first n rows and load selected columns but there is now way to say how to limit the rows to a fixed upper bound.

Ketan Maheshwari
  • 2,002
  • 3
  • 25
  • 31

1 Answers1

1

genfromtxt has an additional skip_footer argument but you need to know total number of lines in your file.

Or you could read the whole file and then slice over the portion you want:

M = loadtxt (filename, delimiter=delimiter) [R1:R2+1,C1:C2+1]

Or

M = loadtxt (filename, delimiter=delimiter, usecols=range(C1,C2+1)) [R1:R2+1,:]

Update: Following hpaulj suggestion you could also define a custom iterator and use that with genfromtxt (in which case you don't need to know the total number of lines in the file and you don't need to construct a bigger matrix first). For example:

class file_iterator ():
    def __init__ (self, filename, begin, end):
        self.file = open (filename)
        self.line_number = 0
        self.begin = begin
        self.end = end

    def __iter__ (self): return self

    def next (self):
        for line in self.file:
            self.line_number += 1
            if self.begin <= self.line_number <= self.end:
                return line
        raise StopIteration

And then use:

M = genfromtxt (file_iterator(filename, R1, R2), delimiter=delimiter, cols=range(C1,C2+1))
Azad
  • 1,050
  • 8
  • 24
  • `genfromtxt` takes any iterable that gives it lines. That could be a generator that returns the specified number of lines. – hpaulj Oct 23 '15 at 15:49
  • 1
    There are several examples in http://stackoverflow.com/questions/26008436/filtering-whilst-using-numpy-genfromtxt – hpaulj Oct 23 '15 at 17:27