सामग्री पर जाएँ

लेसन 24 · Projects and Reference

Project: Clean a Customer List

Build a complete customer list cleaner in Python: profile messy contacts, standardize emails and countries, parse dates, merge duplicates and report results.

In this project you clean a small, invented customer list that mixes many problems from earlier lessons. You will go from profiling to a final report, using only Python's standard library. All names and addresses are made up.

The brief

A fictional shop wants one row per customer for a newsletter. These are the rules agreed with its imaginary owner:

  • The email address identifies a customer. Ignore letter case and outer spaces.
  • A row without a usable email goes to quarantine.
  • Countries must come from an approved mapping. Rows with unknown values are quarantined.
  • Signup dates arrive as YYYY-MM-DD or DD/MM/YYYY. A bad date becomes missing, with a flag.
  • When rows share an email, use the newest non-empty name and country, and the earliest known signup date.
  • A consent value of no on any row wins over yes.

The consent rule is a cautious example. Real consent rules depend on the law and on how and when consent was given, so ask the responsible people.

Step 1: profile

Python Profile the raw customer list

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The profile shows that two emails repeat after normalization, one email is missing and countries use mixed forms. Reading the rows also reveals a malformed email, a country that is not real and a date that does not exist.

Step 2: plan the design

Each row either becomes a clean record or goes to quarantine with every reason. A bad date does not quarantine a row; it sets a flag. Then you group the accepted records by email key, order each group by export date and apply the survivorship rules field by field.

Step 3: the full solution

Python The complete customer cleaning pipeline

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Review the result

Read the output carefully:

  • Ana's two rows merged. The newest name replaced the untidy one, the signup date was kept and the no answer won.
  • Cy's newer row had no name, so the older name filled it. The impossible date was ignored, and the record is flagged for review.
  • Bo, Dee and Eli are in quarantine with reasons, so someone can repair them.
  • Only customers whose merged consent is yes reach the mailing list. Unknown consent is never treated as consent.

Here the newest name for Ana happened to be tidy. If the newest had been lowercase, we would keep it rather than guess at capitalization.

One design gap deserves attention. A quarantined row does not take part in merging. If a quarantined row carried a no, the merged record would not see it. Opt-outs should be collected from all rows with a usable email, including rejected ones. The first extension below asks you to fix this.

Extend it

  1. Collect no answers before quarantine, so an opt-out is never lost.
  2. Write tests for parse_date and merge, including a group with no known dates.
  3. Handle rows with an empty exported value without crashing the sort.
  4. Suggest a repair for the malformed email, but do not apply it automatically.
  5. Move the country mapping into a CSV file.
  6. Add a data dictionary and a run report.

Common mistake: repairing an email by guessing, such as adding .com to bo@example. A guessed address may belong to a stranger.

Think about the design

The solution separates three decisions that are easy to mix up. parse_date only knows about formats. clean_row decides whether a single row is usable. merge decides how several usable rows become one customer. When a rule changes, you can find the one function that holds it, and you can test each part with a few small inputs.

Look also at what the output keeps. source_rows lets you trace each customer back to the file, and review_date stops an uncertain record from looking complete. Quarantined rows keep a list of reasons, so a person can repair several problems at once instead of discovering them one at a time.

Finally, consider the sorting rule. Two rows exported on the same day would keep their original file order, because Python's sort is stable. That may be acceptable, but it is a hidden tie-breaker. For real data, add an explicit one, such as the row number, and write it into the brief.

Check the checks

Change the input and predict the result before you run it. Add a second Dee Park row with an approved country. What should happen to her? Then add two rows with the same email and different countries. Which one wins, and is that what the brief says? Small experiments like these show whether you understand your own rules, and they make good test cases.

You should also consider how this script would handle personal data in real life. Printing full records is fine for invented data. In production, log row numbers and reasons instead, restrict access to the quarantine file and delete working copies when they are no longer needed.

Share the outcome

Give the owner the clean list, the quarantine reasons and the open questions together. A clean file on its own hides how many people were left out.

Check your understanding

Quick check · 3 questions

of 3 answered You scored / 3 · perfect!

  1. 1. Why does a no answer win over yes in this project?

    This project's rule favors the safer choice. Real rules must come from the responsible people.
  2. 2. Why is an impossible signup date not a reason to quarantine the row?

    The brief says to keep the customer and record that the date needs review.
  3. 3. What does the final assert check?

    It is a count check. It does not prove that the values themselves are right.

Summary

  • Agree on rules in writing before you code them.
  • Separate row cleaning from merging.
  • Use field-level survivorship rules and keep source row numbers.
  • Treat unknown consent as no permission, never lose an opt-out and account for every row.

Next: Project: Repair a Sales Export in SQL

आपकी प्रगति इसी ब्राउज़र में सेव होती है। खाते की ज़रूरत नहीं है।