How to total a CSV column with csv.DictReader
· 1 min read · Updated March 13, 2026 · beginner
python csv dictreader files
Use csv.DictReader when you want to read rows by column name instead of numeric index. It makes small reporting tasks easier to read and less fragile when column order changes.
Recipe
import csv
def total_column(path, column_name):
total = 0.0
with open(path, newline="", encoding="utf-8") as file:
reader = csv.DictReader(file)
for row in reader:
value = row.get(column_name, "").strip()
if not value:
continue
total += float(value)
return total
sales_total = total_column("sales.csv", "amount")
print(f"Total: {sales_total:.2f}")
This pattern works well for CSV exports where the header row is reliable but some cells may be blank. row.get() avoids a crash if the key is missing, and skipping empty values keeps the loop simple.
If you need exact money arithmetic, replace float() with Decimal. For quick totals in scripts and one-off reports, float() is usually enough.
With this input:
customer,amount
Acme,19.99
Beta,12.50
Gamma,
Delta,8.00
The recipe prints:
Total: 40.49