Skip to content

Clean Messy Text in a CSV

The email " [email protected] " does not match [email protected] in a join or in Remove Duplicates, although both belong to the same person. Strip the surrounding spaces, then lowercase the text, so equal addresses become equal values.

You'll use: File Input → String Transform → String Transform → File Output.

  1. 1Input
    File Input
    customers.csv
  2. 2Transform
    String Transform
    email · Strip spaces
  3. 3Transform
    String Transform
    email · Lowercase
  4. 4Output
    File Output
    customers_clean

Sample data

Save this as customers.csv. Customers 1 and 4 have the same address written two ways, customer 3 has no email, and customer 5 has only spaces.

csv
customer_id,email
1,"  [email protected] "
2,[email protected]
3,
4,[email protected]
5,"   "

Steps

  1. On Datasets, upload customers.csv.
  2. In a new flow, add File Input. Set File type to CSV and Dataset to customers.csv.
  3. Add String Transform. Set Column to email and click the Strip spaces operation. It removes spaces at the start and end of each value.
  4. Add a second String Transform. Set Column to email and click Lowercase. One node applies one operation, so the two steps need two nodes.
  5. Add File Output. Set File type to CSV (.csv) and Dataset name to customers_clean. Connect the nodes in order and click Run.

Prefer to describe it?

With a provider key set up, upload the sample data, open a blank flow in the same project and ask the Flow assistant, for example: "trim the spaces in the email column and lowercase it, then write a CSV." Review the proposed nodes and settings, then choose Apply and save. We ran this prompt on the sample data; see what the assistant built. Its flow can differ from the steps above in node order and names, so check each setting. The assistant never runs a flow, so click Run yourself.

Result

customer_idemail
1[email protected]
2[email protected]
3null
4[email protected]
5empty text ("")

Customer 1 and customer 4 emails differ by spaces and by case; after Strip spaces they still differ by case, and after Lowercase they are equal

Check the result

Save the flow, click Preview, select the second String Transform node and click Run preview. Customers 1 and 4 should show the same [email protected]. If customer 1 still shows spaces or capitals, one of the two String Transform nodes is not in the chain before the node you preview. Their order does not matter.

Pitfalls

  • Clean both sides of a join. Apply the same steps to the key column of each input before a Join, or the keys still differ.
  • Only spaces become empty text, not null. Customer 5 ends up as "", which Fill Nulls and Coalesce do not treat as missing. In a CSV output an empty text and a null can look the same, so check them in the preview.
  • Strip spaces does not touch inner spaces. ada @example.com keeps its space. Use the Find & Replace operation for characters inside the value.
  • Lowercase only where case carries no meaning. Keep passwords, tokens and case-sensitive IDs as they are. Cleaning does not check that an address is valid or deliverable.

Exported Python

Click Export in the editor toolbar. The pandas and polars tabs show this code for the flow. It reads customers.csv from the script's folder and writes customers_clean.csv.

python
import pandas as pd

df_customers = pd.read_csv('customers.csv')

df_customers = (
    df_customers.assign(email=lambda _d: _d['email'].astype('string').str.strip())
    .assign(email=lambda _d: _d['email'].astype('string').str.lower())
)

df_customers.to_csv('customers_clean.csv', index=False)

See also