We have an input file in which strings are stored. Our task is to put into the output file only those lines that are not repeated with the previous ones. The teacher forbade the use of arrays, so we should only compare strings using filename.readLine (). Sample input file:

apple apple apple apple banana bargain brick brick sample sample simple text text text 

Example file output:

 apple banana bargain brick sample simple text 

Here is my code, which for some reason simply reprints the original file:

 fileFrom = open ('data.txt', 'r') fileTo = open ('result.txt', 'w') line = fileFrom.readline() while line != '': if line == fileFrom.readline(): line = fileFrom.readline() else: fileTo.write(line + '\n') line = fileFrom.readline() fileFrom.close() fileTo.close() 

If you remove line = fileFrom.readline() in a loop, then it generally reprints only the first line. Obviously, the if condition is not satisfied, but I cannot understand why.

3 answers 3

If the array cannot be used, then the previous line can be written to the prevline variable:

 fileFrom = open('data.txt', 'r') fileTo = open('result.txt', 'w') line = fileFrom.readline() prevline = "" while line != '': line = fileFrom.readline() if prevline != line: fileTo.write(line) prevline = line 
  • Thanks, your approach worked. Only here it is strange that it works on all computers except my own. - lili
  • @lili try running the script as administrator - Frank

Since duplicate lines go in a row in a file, you can use itertools.groupby() to find groups of identical adjacent lines and write only one line from each group:

 fileTo.writelines(uniq for uniq, _ in itertools.groupby(fileFrom)) 

Since the lines are compared literally, the last line must have '\n' in order to be considered as a repetition.

You can also implement it with your hands:

 prev = None for line in fileFrom: if line != prev: prev = line fileTo.write(line) 

Both solutions use the fact that the file is an iterator over lines in Python.

    This is the code I got in the end. It works. Thank you very much!

     fileFrom = open('data.txt', 'r') fileTo = open('result.txt', 'w') line = fileFrom.readline() old_line = '' while line != '': line = fileFrom.readline() if old_line != line: fileTo.write(line) old_line = line