Question: Here is a Python program that reads customer data from a list of dictionaries, processes it , and writes the results to a new file.

Here is a Python program that reads customer data from a list of dictionaries, processes it, and writes the results to a new file. The program uses control flow, loops, functions, and classes to manipulate data stored in lists and dictionaries. It also utilizes modules to handle file input and output:
class Customer:
def __init__(self, name, age, email, phone):
self.name = name
self.age = age
self.email = email
self.phone = phone
def read_customer_data(filename):
"""
Read customer data from a file and return a list of Customer objects.
"""
customers =[]
try:
with open(filename,'r') as file:
for line in file:
data = line.strip().split(',')
if len(data)==4:
name, age, email, phone = data
customer = Customer(name, int(age), email, phone)
customers.append(customer)
except FileNotFoundError:
print(f"Error: File '{filename}' not found.")
return customers
def write_customer_data(customers, filename):
"""
Write customer data to a file.
"""
try:
with open(filename,'w') as file:
for customer in customers:
file.write(f"{customer.name},{customer.age},{customer.email},{customer.phone}
")
print(f"Customer data has been written to '{filename}' successfully.")
except Exception as e:
print(f"Error writing to file '{filename}': {e}")
def process_customer_data(customer_data):
"""
Process customer data (for example, Sort the customer data based on age).
"""
sorted_customers = sorted(customer_data, key=lambda x: x.age)
return sorted_customers
def main():
input_filename = 'customer_input.txt'
output_filename = 'customer_output.txt'
# Read customer data from file
customers = read_customer_data(input_filename)
# Process customer data if necessary
customers = process_customer_data(customers)
# Write processed customer data to file
write_customer_data(customers, output_filename)
if __name__=="__main__":
main()
Explanation:
Customer class: Represents a customer with attributes name, age, email, and phone number.
read_customer_data: Reads customer data from a file provided as input and returns a list of Customer objects.
write_customer_data: Writes customer data to a file provided as output.
process_customer_data: Processes customer data, in this case, sorts the customer data based on age.
main function: Coordinates the entire process. It reads customer data from the input file, processes it if necessary, and then writes the processed data to the output file.Write a pseudocode and control flow chart for the above solution.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!