liveBook · Manning

Header

As explained in Tiny Python Projects: Learn coding and testing with puzzles and games

A header is a record at the beginning of a tabular file that names or describes the fields in subsequent records.

A header is the first line of a CSV file. It lists field names such as exercise and reps, allowing later values to become named dictionary fields instead of anonymous positions. section 19.1.2

A simple parser reads the header, removes its newline, splits it on commas, and pairs the resulting names with each data row using zip(). Converting those pairs to a dictionary produces records such as {'exercise': 'Burpees', 'reps': '20-50'}. section 19.1.2

Reading headers in Python

with open('inputs/exercises.csv') as fh:\n headers = fh.readline().rstrip().split(',')\n records = [dict(zip(headers, line.rstrip().split(','))) for line in fh]

$ head -3 inputs/exercises.csv
exercise,reps
Burpees,20-50
Situps,40-100

Inspect the first three lines of a CSV to see the header preceding its data records. section 19.1.2

How Tiny Python Projects: Learn coding and testing with puzzles and games covers it

How deep it goes
Explains reading, trimming, splitting, and pairing headers, while deferring malformed-header and missing-data decisions to error handling.
Examples
Shows exercise and reps headers, Burpees data, Python readline(), split(), zip(), and dictionary construction.
What you can do after
Builds a basic Python CSV reader that maps each row’s values to names taken from the header.
How it connects
Connects CSV files with dictionaries, field names, delimited text files, and zip().

Also explained in

Compare how 3 books explain header →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Tiny Python Projects: Learn coding and testing with puzzles and games. The section links point to the parts of the book it draws on.