#!usr/bin/python3
#=====================================================================================
# Display the first episode for every title
#=====================================================================================

#input_file  = "titles.csv";
#output_file = "titles2.csv";
input_file  = "C:/avails/Product Avails 9-12-2017.csv";            #input file
output_file = 'C:/avails/Season Avails 9-12-2017.csv';             #output file

#=====================================================================================
# readFile: Read input file, and if first episode in season store into a 2 dim array
#=====================================================================================
def readWrite(infile, outfile):

    input  = open(infile, 'r')                          #Open file for reading
    output = open(outfile,'w')                          #Open file for writing
    
    prev_title = ''
    for rec in input:                                   #loop thru the data rows
        rec   = rec.rstrip()                            #get rid of the rightmost trailing newline
        array = rec.rsplit(',')                         #split the record on ,
        if not array[1]:
            array[1] = array[2]
#       array[2] = ''
        if (array[1] != prev_title):                    #if title != prev_title
            rec = ','.join(array)                       #join the array with ,
            output.write(rec + '\n')                    #write record
            prev_title = array[1]                       #save it in prev_title

    input.close()                                       #close the file
    output.close()                                      #close the file
    
#=====================================================================================
# main code
#=====================================================================================

readWrite(input_file,output_file)