Python Tutorial

Python Project: CSV Report

Parse rows of sales data, compute totals per product, and print a sorted report.

What you are building

You parse a small CSV of product, quantity, and unit price. The program multiplies quantity by price for each row, sums revenue per product, and prints a report sorted by revenue, highest first.

Stay in the Python editor at /try. This track is not HTML, C, or C++. The CSV lives in a multiline string. You do not read a file from disk, and you do not install packages. The standardcsv module is enough.

Skills used

  • csv.DictReader on a string via io.StringIO
  • int and float conversions on cells
  • A dict of running revenue per product
  • sorted with a key and reverse=True
  • A printed table of product, units, and revenue

Parse a CSV string

In this editor there is no sales.csv on disk. Put the same text in a string. Wrap it withStringIO so csv can read it like a file. DictReader uses the header row as keys.

Example

import csv
from io import StringIO

raw = """product,qty,price
Mug,3,4.50
Notebook,2,8.00
Pen,10,1.25
"""

reader = csv.DictReader(StringIO(raw))
for row in reader:
    print(row["product"], row["qty"], row["price"])

Cells arrive as strings. "3" is not 3 until you call int. "4.50"needs float. Convert once when you walk the rows.

Revenue per product

Line revenue is quantity times unit price. Two Mug rows must add, not overwrite. Store a pair per product: units sold and money earned. A dict keyed by product name holds that pair.

Example

import csv
from io import StringIO

raw = """product,qty,price
Mug,3,4.50
Notebook,2,8.00
Mug,1,4.50
"""

totals = {}
reader = csv.DictReader(StringIO(raw))
for row in reader:
    name = row["product"]
    qty = int(row["qty"])
    price = float(row["price"])
    units, revenue = totals.get(name, (0, 0.0))
    totals[name] = (units + qty, revenue + qty * price)

print(totals)

csv.reader would give lists. DictReader gives names, so a later column swap in the header is easier to see. Skip blank lines. Do not write a parser that splits on every comma if a future cell might contain a comma; the csv module already handles quotes.

Complete program

Five sales rows, three products. The report sorts by revenue. Mug earns 18.00, Notebook 24.00, Pen 12.50. Notebook should print first. Run the whole script at /try.

Example

import csv
from io import StringIO

RAW = """product,qty,price
Mug,3,4.50
Notebook,2,8.00
Mug,1,4.50
Pen,10,1.25
Notebook,1,8.00
"""

totals = {}
reader = csv.DictReader(StringIO(RAW))
row_count = 0
for row in reader:
    name = row["product"].strip()
    qty = int(row["qty"])
    price = float(row["price"])
    units, revenue = totals.get(name, (0, 0.0))
    totals[name] = (units + qty, revenue + qty * price)
    row_count += 1

ranked = sorted(totals.items(), key=lambda item: item[1][1], reverse=True)
grand_units = sum(units for units, _ in totals.values())
grand_rev = sum(revenue for _, revenue in totals.values())

print("CSV sales report")
print("Rows parsed:", row_count)
print()
print(f"{'product':<12} {'units':>6} {'revenue':>10}")
print("-" * 30)
for name, (units, revenue) in ranked:
    print(f"{name:<12} {units:>6} {revenue:>10.2f}")
print("-" * 30)
print(f"{'TOTAL':<12} {grand_units:>6} {grand_rev:>10.2f}")
ProductUnitsRevenueHow
Notebook324.002 + 1 at 8.00
Mug418.003 + 1 at 4.50
Pen1012.5010 at 1.25
TOTAL1754.50sum of the three

Common mistakes

  • Opening a real path such as open("sales.csv") in this editor. There is no project folder file. Use the multiline string.
  • Sorting by product name when you wanted money. The key is item[1][1]: the revenue inside the tuple.
  • Multiplying strings: "10" * 3 repeats text. Convert types first.
  • Forgetting the header. If you use csv.reader and do not skip row 1, int("qty")crashes.
  • Installing a spreadsheet library. This report is stdlib only: csv, io, dicts.

How to extend / Practice tasks

Leave the five demo rows in place until 54.50 matches.

  1. Add a line Mug,2,4.50 and confirm Mug units become 6 and revenue 27.00, with a new grand total of 63.50.
  2. Sort by units instead of revenue and print which product moves to the top (Pen).
  3. Skip any row whose quantity is 0 or whose price is negative. Test by insertingSticker,0,1.00 and Lamp,-1,20.00 and checking that TOTAL units stay 17.

📘 Real-World Deep Dive

Turning a CSV into a summary report is the single most common "first real task" a Python developer is handed at work. It ties together file reading, per-group aggregation, and clean formatted output.

What to build

Read a sales CSV, total revenue per region, and print a tidy report — streaming rows so it works whether the file has 10 lines or 10 million.

Real-Life Example

import csv, io
from collections import defaultdict

SAMPLE = """region,product,amount
north,widget,120
south,widget,80
north,gadget,200
south,gadget,50
"""

def report(f) -> dict:
    totals = defaultdict(float)
    for row in csv.DictReader(f):          # streams one row at a time
        totals[row["region"]] += float(row["amount"])
    return dict(totals)

for region, total in sorted(report(io.StringIO(SAMPLE)).items()):
    print(f"{region:6} {total:8.2f}")

csv.DictReader streams rows lazily, so memory stays flat even on huge files — no list(reader) needed.

Expected Output

north   320.00
south   130.00

Common mistakes

  • line.split(",") breaks on quoted fields that contain commas — always use the csv module, never manual splitting.
  • list(csv.reader(f)) pulls the whole file into RAM; iterate the reader directly to stream.
  • Every CSV value is a string — forgetting float(row["amount"]) concatenates text instead of adding numbers.

🚀 Performance & Best Practices

  • Streaming with DictReader keeps memory constant regardless of file size.
  • For heavy numeric crunching or joins across files, pandas' read_csv + groupby is far faster and shorter — a natural next step.
  • Write the report with csv.writer (not string formatting) if it feeds another program.

🧪 Try It Yourself

  1. Read a real file path from sys.argv instead of the embedded sample.
  2. Add a second breakdown (per product) and print both tables.
  3. Re-implement the aggregation with pandas and compare the line count.

FAQ: Python Project: CSV Report

Common questions about this page.

What is Python Project: CSV Report?

Python Project: CSV Report is a Python Projects lesson that explains python csv report project in Python. Parse rows of sales data, compute totals per product, and print a sorted report. Copy the samples and run them in the Python editor. It is written for beginners who want a clear definition and working examples.

Should I run python csv report project examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn python csv report project in this Python Projects Python lesson (Python Project: CSV Report).

How do I use python csv report project in Python?

To use python csv report project in Python, follow the examples on this StudyGrid page. Copy a snippet, run it in the browser, then Download and run it locally for better learning. Change the values and compare the output.

What is the syntax of python csv report project?

This Python Project: CSV Report tutorial shows python csv report project syntax with short Python examples. Use the code blocks in this lesson for the exact statements, then try them in your editor.

Python Project: CSV Report example for beginners

Yes. This page includes a beginner python csv report project example you can copy and run. It is designed for searches such as "python csv report project for beginners", "python csv report project example", and "how to use python csv report project".

What are common mistakes with python csv report project?

Common python csv report project mistakes include wrong syntax, mixing types, and skipping practice. Work through this Python Projects chapter in order, run every example, and check the output before moving on.

Why should I learn python csv report project?

Python Project: CSV Report is used in real Python work. Learning python csv report project helps you write clearer programs and continue the Python Projects tutorial on StudyGrid.

Is Python Project: CSV Report free to learn online?

Yes. You can learn python csv report project free on StudyGrid (studygrid.in). This chapter is part of the Python Projects path and includes examples, syntax, and next-step links.