Iterate over text file, discarding some lines via context manager
nomail at invalid.com
Fri Nov 28 18:06:02 CET 2014
Here is a solution with a custom iterator which operate on files.
I tested it with a small file. Just a remark, there are no empty line
on a file, there is at least '\n' at the end of each lines but maybe the
last one. If readline() got an emptyline, then the end of file has been
def __init__(self, f):
self.f = f
self.line = ""
self.line = self.f.readline()
if (self.line == ''):
if not(self.line.startswith('#') or self.line.isspace() or self.line == '\n'):
f = open('test.txt', 'r')
for L in MyFileItr(f):
More information about the Python-list