#======================================================================
# Read data from a file to a 2dim list - one record at a time
# Update certain value(s), Display the data
# Write the 2dim list to the same file 
#======================================================================

fileIn  = 'contact.file'		    #input file
fileOut = 'contact.file'		    #output file (the same)

old_addr = input("Enter previous address: ")		#update address from old
new_addr = input("Enter new address.....: ")		#to new address 

list2 = [ ]							#2 dimensional list to store the file data

#======================================================================
# Read data from file into 2 dimensional list
#======================================================================
def read_file_into_list(fileIn, list2):

      input  = open(fileIn, 'r')					#Open file for reading

      for rec in input:						#loop thru the data rows
            rec  = rec.rstrip( )					#strip trailing newline
            row = rec.split('//')					#split the record on // and return a list
#          (fn, ln, addr, email, phone) = row			#get individual elements (if needed)
            list2.append(row)					#append each row to the 2dim list

      input.close( )						#close the file

#======================================================================
# Update the Data in the 2 dimensional list
#======================================================================
def update_2dim_list(list2, old_addr, new_addr):

      for row in list2:						#loop thru the data rows
          print(row)
          (fn, ln, addr, email, phone) = row			#breakup the row into a tuple 
          if old_addr.lower()  in  addr.lower( ):					#if found old address 
                 row[2] = new_addr					#change it to new_addr

#=======================================================================
# Display the content of the 2 dimensional list (this method is optional)
#=======================================================================
def display_2dim_list(list2):

      for row in list2:					#loop through each row in the 2dim list
            for element in row:				#loop through each element in the row 
                  print(element, "\t ",end="")
            print( )

#======================================================================
# Write the data from 2 dimensional list into file
#======================================================================
def write_list_into_file(list2, fileOut):

      output = open(fileOut, 'w')				#Open file for writing

      for row in list2:						#loop thru the data rows
            rec =  '//'.join(row)					#join all the elements using //
#          (fn, ln, addr, email, phone) = row			#breakup the row into a tuple (if need be)
#          rec  = fn +'//'+ ln +'//'+ addr +'//'+ email +'//'+ phone	     #concatenate all the elements
            rec += '\n'						#add a newline character

            output.write(rec)					#write the record

      output.close( )						#close the file

#======================================================================
# main code
#======================================================================

read_file_into_list(fileIn, list2)			       #read file into 2 dim list

update_2dim_list(list2, old_addr, new_addr)	       #update the list from value, to value

display_2dim_list(list2) 				       #display the 2 dim list (optional)

write_list_into_file(list2, fileOut)			       #save the 2 dim list into a new file