Check a CSV Header Before a Data Pipeline Load
Run a Python exercise that rejects extra, reordered and missing headers, then test what a header check still cannot tell you.
Why check a CSV header before loading?
A source export can add a column or change its order while keeping the same filename. If a loader assumes positions, the wrong value can reach the wrong field. Agree on a contract before reading data rows. This exercise tests an intentionally strict contract: exactly two named columns, in a specified order.
Run a small Python check
Use Python 3 and save the following code in a local practice file. It reads invented strings in memory. It creates no cloud resources, admissions or database records.
import csv
import io
EXPECTED = ["order_id", "amount"]
def header_matches(text):
reader = csv.reader(io.StringIO(text, newline=""))
return next(reader, None) == EXPECTED
samples = {
"correct": "order_id,amount\n1,100\n",
"extra": "order_id,amount,currency\n1,100,INR\n",
"reordered": "amount,order_id\n100,1\n",
"empty": "",
}
for name, text in samples.items():
print(name, "ACCEPT HEADER" if header_matches(text) else "REJECT HEADER")
Expected output:
correct ACCEPT HEADER
extra REJECT HEADER
reordered REJECT HEADER
empty REJECT HEADER
The four cases and three additional boundary checks were executed with Python 3.12.14 before publication. The output above matched. The cloud loading step is outside this example.
Explain each decision
The first row from csv.reader is a list of strings. Comparing it with EXPECTED checks both the names and their order. The extra-column file fails because the lists differ. The reordered file fails because this particular contract is positional. An empty input returns None through next(reader, None), so it also fails.
Column order is a deliberate teaching constraint. A loader that maps by names may support reordered columns, but it still needs an explicit policy for duplicates, missing fields and unexpected fields. Replacing this comparison with a set comparison can conceal duplicate header names. Do not change the rule without changing the contract and tests together.
A matching header does not validate the file
Try a file with only the header and no data rows. This function accepts its header. A file with a correct header followed by a one-field data row also passes this narrow check. Neither result says the file is ready to load. Row width, required values, numeric conversion, duplicate order identifiers and reconciliation require separate checks.
Python's CSV reader handles delimiter and quoting rules; avoid splitting CSV lines on commas yourself. The default reader returns strings, so the amount text 100 has not become a validated numeric amount. For real files, choose an encoding and open with newline="" as the Python documentation recommends. File size, malformed CSV and decoding errors need handling in a fuller ingestion process.
Practise a contract change
Add a currency column to EXPECTED and update the synthetic fixture. Predict which cases now pass before running them. Next, add a duplicate order_id header and explain why it should fail. Write down who would approve a source change in a real pipeline. Keep a rejected fixture for debugging; do not silently drop a column just to make a load succeed.
Prepare for a course discussion in Chennai or Tambaram
Before an online Data Engineering demo, run these strings on your own laptop and bring the output plus one failing fixture. Ask how the header check would fit before a GCS-to-BigQuery load, and what should happen when the source contract changes. Tambaram is the admission enquiry contact. Confirm current format and schedule with the team before planning a visit or online session.
Read the GCS-to-BigQuery load runbook guide to connect the check with a wider pipeline discussion. Explore the Data Engineering course syllabus and learning options and request a relevant demo.