Search code examples
pythonfiletextrows

How to pythonically order triplets of line into columns and avoiding doing that for doublets of line?


Sorry if my question is not so clear. Basically what I am trying to do is to identify by reading the file 3 non empty lines in a row (triplet) and for each triplet, sort them into another file in columns (as explained below). One tricky part is to do this only for triplets and not doublets (two non empty lines in a row).

Input:

line1
line2
line3
(empty line)
(empty line)
line4
line5
(empty line)
line6
line7
line8
(empty line)
(empty line)
line9
line10
(empty line)
line11
line12
line13
output: 
line1 line2 line3 
line6 line7 line8 
line11 line12 line13

You can notice that doublets can follow or be followed by two empty lines. I would appreciate any kind of help with my current problem as I am a python debutant and I am stuck with my code after hours attempting something. Thank you:)


Solution

  • I would suggest to count the number of non-empty lines you have read so far. If you encounter an empty line and you know you have only read two non-empty lines since the last empty line, you can throw the last lines away. However, if you find that you have read three non-empty lines, you write them to the new file.

    with open('input_file.txt', 'r') as input_file:
        with open('output_file.txt', 'w') as output_file:
            non_empty = 0
            previous_lines = []
            for line in input_file.readlines():
                if line == '\n':
                    if non_empty == 3:  # found a triple
                        output_file.write(' '.join(previous_lines) + '\n')
                    # if it's not a triple, just ignore it
                    # reset the counter so we can detect the next triple
                    non_empty = 0
                    previous_lines = []
                else:
                    # record a non-empty line for later use
                    previous_lines.append(line.replace('\n', ''))
                    # and important: count how many non-empty lines there are
                    non_empty += 1