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