parsing - how to read from a particular line upto a particular line in python -
i haave file , want read specific part of it. file.
..... ..... admin server interface begins ... .... .... .... .... admin server interface ends .... ....
i want read part of file between 'admin server interface begins' till 'admin server interface ends'. found way of doing in perl can't find way in python.
in perl
while (<inp>) { print $_ if(/^adminserver interface definitions begins/ .. /^adminserver interface definitions ends/); }
could anyonle please help.
you can read file line line , gather in between markers.
def dispatch(inputfile): # if separator lines must included, set true need_separator = true new = false rec = [] open(inputfile) f: line in f: if "admin server interface begins" in line: new = true if need_separator: rec = [line] else: rec = [] elif "admin server interface ends" in line: if need_separator: rec.append(line) new = false # if not need process further, uncomment following line #return ''.join(rec) elif new: rec.append(line) return ''.join(rec)
the code above return data if input file not contain ending separator (admin server interface ends
). can amend last return
condition if want catch such files:
if new: # handle case there no end separator print("error in input file: no ending separator") return '' else: return ''.join(rec)
Comments
Post a Comment