Issue
This
This line
This line has
This line has five
This line has five strings
I need the script to delete lines 1 through 4 and just print line 5
This looks for exact lines so did not work for me:
lines_seen = set() # holds lines already seen
outfile = open(outfilename, "w")
for line in open(infilename, "r"):
if line not in lines_seen: # not a duplicate
outfile.write(line)
lines_seen.add(line)
outfile.close()
Solution
with open(infilename) as fin:
lines = list(map(str.rstrip, fin))
i = 0
while i < len(lines):
while i + 1 < len(lines) and lines[i + 1].startswith(lines[i]):
i += 1 # duplicate, so skip
print(lines[i])
i += 1
Feel free to send those surviving lines to an output file, if desired.
Answered By - J_H
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.