57.7k views
5 votes
Gimme Shelter Roofers maintains a file of past customers, including a customer number, name, address, date of job, and price of job. It also maintains a file of estimates given for jobs not yet performed; this file contains a customer number, name, address, proposed date of job, and proposed price. Each file is in customer number order.

Required:
Design the logic that merges the two files to produce one combined file of all customers whether past or proposed with no duplicates; when a customer who has been given an estimate is also a past customer.

User Zslayton
by
5.4k points

1 Answer

5 votes

Answer:

Hence the complete implementation of python code that reads two files and merges them together.

def merge_file(past_file_path,proposed_file_path, merged_file_path):

past_file_contents=load_file(past_file_path)

proposed_file_contents=load_file(proposed_file_path)

proposed_customer_name = []

for row in proposed_file_contents:

proposed_customer_name.append(row[1])

with open(merged_file_path,'w') as outputf:

outputf.write("Customer Number, Customer Name, Address\r\\")

for row in proposed_file_contents:

line = str(row[0]) +", " + str(row[1]) + ", " + str(row[2]) +"\r\\"

outputf.write(line)

for row in past_file_contents:

if row[1] in proposed_customer_name:

continue

else:

line = str(row[0]) + ", " + str(row[1]) + ", " + str(row[2]) + "\r\\"

outputf.write(line)

print("Files merged successfully!")

# reads the file and returns the content as 2D lists

def load_file(path):

file_contents = []

with open(path, 'r') as pastf:

for line in pastf:

cells = line.split(",")

row = []

for cell in cells:

if(cell.lower().strip()=="customer number"):

break

else:

row.append(cell.strip())

if len(row)>0:

file_contents.append(row)

return file_contents

past_file_path="F:\\Past Customer.txt"

proposed_file_path="F:\\Proposed Customer.txt"

merged_file_path="F:\\Merged File.txt"

merge_file(past_file_path,proposed_file_path,merged_file_path)

User Holger Peine
by
5.0k points