Parsing text files in Python

For this exercise, the goal was to open a text file, parse through the list of words and numbers, and reorder them. Then, sort them so that the lines or elements in the text file are arranged in a pyramid structure. So instead of one word/number pair per line, there’s now an increasing number of word/number pairs on each line, ie. 113 keen on line 1, and 114 great 115 okay on line 2, and 3 elements on line 3, and so on. Below is the script I came up with:

CodeToHTML
############################################################################
'''
Alex Corll
3/30/2024
Decode a message exercise
'''
###########################################################################
import os
import sys


def decode():
     # Read the content of the file
    text = sys.argv[1]
    #print(text)
    try:
        with open(text'r'as textfile:
            # r strip will strip our new line \n escape character
            content = [line.rstrip() for line in textfile]
            num_lines = len(content)
            # Array to hold all our numbers
            txt_numbers = []
            txt_strings = []
            
            # Loop over our parsed content and print each line
            for k in range(num_lines):
                # Use a place holder 2d array to add our elements
                # This gets overwritten each time our loop runs
                split_content_line = content[k].split()

                # Append text numbers and convert to int
                txt_numbers.append(int(split_content_line[0]))

                txt_strings.append(split_content_line[1])

            # Debug print calls
            print(f'Our complete list: {content}')
            print(f'Our list has this many lines: {num_lines}')


            print(f'Our list of numbers: {txt_numbers}')
            print(f'Our list of strings: {txt_strings}')

            # Here we will create a tuple list from our 2 lists
            sorted_txt_numbers = sorted(txt_numbers)
            txt_zipped = list(zip(txt_numbers,txt_strings))
            
            print(f'Our zipped list: {txt_zipped}')
            # Create a list to hold our tuple queries when 
            # we find a match.  If there's a match, add to this list.
            # The lambda sort will rearrange our zipped list in order
            sorted_list = list(sorted(txt_zippedkey=lambda xx[0]))

            # Print the sorted list
            print(f'Our sorted list: {sorted_list}')
            print(f'i in list: {sorted_list[0]}')
            print(f'End of list: {sorted_list[num_lines - 1]}')
            pyramid_sort_lst = []
            i = 0
            row_length = 1
            
            length_sort = len(sorted_list)
            print(f'Length sort: {length_sort}')
            # While i is not equal to the last element in the list, 
            # keep processing our list
            # Everytime this loop runs we will append 1 more element
            # of the list to the next line
            while i < length_sort:
                if i == num_lines:
                    exit()
                else:
                    print(f'Skipped to this element: {i}')
                    print(f'Currently on row element {sorted_list[i]}')
                    print(f'Current row length: {row_length}')
                    
                    if row_length == 1:
                        pyramid_sort_lst.append([sorted_list[i]])
                        print(f'First entry in pyramid: {pyramid_sort_lst}')

                    if row_length > 1:
                        pyramid_sort_lst.append(sorted_list[(i):
                                                    (i + row_length)])            
                    # As row length increases, offset i accordingly
                    if i > length_sort:
                        exit
                    else:
                        i += row_length
                    
                    # Increase our maximum row length with each iteration
                    row_length += 1

            # Print the list sorted in our pyramid format
            print(f'Our pyramid sorted list: {pyramid_sort_lst}')
            print(f'Length of pyramid: {len(pyramid_sort_lst)}')
            for newline in pyramid_sort_lst:
                # Printing the line without center formatting
                print(f'New line of our pyramid: {newline}')
            # Declare a string array that will hold our decoded words
            end_string_array = []
            # Loop over our list and print each line, checking line length
        for line in pyramid_sort_lst:
                # Printing the line without center formatting
                print(f'New line of our pyramid: {line}')
                # print our line length for each line
                print(f'Length of line: {len(line)}')
                line_end = len(line)
                # Extract the string at the end of the line
                if line_end < 2:
                    print(f'Short line: {line}')
                    current_line_end = line[line_end - 1]
                    # Unpack our tuple on the current line
                    end_numend_string = current_line_end
                    print(f'Current line end word: {end_string}')
                    end_string_array.append(end_string)

                else:
                    print(f'Chunk at end of line:{line[line_end - 1]}')
                    current_line_end = line[line_end - 1]
                    print(f'Current line end: {current_line_end}')

                    # Unpack our tuple on the current line
                    end_numend_string = current_line_end
                    print(f'Current line end word: {end_string}')

                    # Append this string to a new string
                    end_string_array.append(end_string)

        # Print our new list, our extracted secret message
        print(f'Our list of decoded words at row end: {end_string_array}')
                        
    except FileNotFoundError:
        print(f"File {text} not found."

if __name__=="__main__"
     decode()