Home>
AAA
BBB
CCC
DDD
DDD
DDD
EEE
FFF

When there is such a file, DDD is continuous, so I want to delete the second and third DDD.
I know the strings written on successive lines.

AAA
BBB
CCC
DDD
EEE
FFF
path = file1
f = open (path)
line = f.readlines ()
f.close ()
for list in line:
    with open (path) as f:
         next (f)
         for line2 in f
             if line2 == list:

In this way, we tried to compare the first and n + 1 lines of the same file in order.
However, with this method, you have noticed that you cannot deal with the case where the same line continues more than once, and i am stuck.

Tell me how to do it well.

  • Answer # 1

    It's an unknown code.

    linehas already read the entire file, sofor list in line:will look at each line.
    If it is the same as the previous line, do not output.
    The previous line to be compared must be remembered in another variable in the previous loop.