CodingNic

Files, Errors, and Automation

Working with CSV Files

Files, Errors, and Automation 26 min read

Working with CSV Files

Working with CSV Files

Some files store data in rows and columns.

This is common for:

  • Reports
  • Student scores
  • Product lists
  • Sales data
  • Spreadsheets

A popular format for this is CSV.

What Is a CSV File?

CSV means Comma-Separated Values.

Values are separated by commas.

Example file: students.csv

text
name,score
Tom,85
Sara,92
Ali,70

Each line is one row.

Why CSV Files Matter

CSV files are useful because they:

  • Organize data clearly
  • Work with spreadsheet apps
  • Are easy to share
  • Are great for reports

Import the csv Module

Python has a built-in module called csv.

python
import csv

Reading a CSV File

python
import csv

with open("students.csv") as file:
    reader = csv.reader(file)

    for row in reader:
        print(row)

Output

text
['name', 'score']
['Tom', '85']
['Sara', '92']
['Ali', '70']

Each row becomes a list.

Skip the Header Row

The first row often contains column names.

python
import csv

with open("students.csv") as file:
    reader = csv.reader(file)

    next(reader)

    for row in reader:
        print(row)

Output

text
['Tom', '85']
['Sara', '92']
['Ali', '70']

Using DictReader

DictReader uses column names as keys.

python
import csv

with open("students.csv") as file:
    reader = csv.DictReader(file)

    for row in reader:
        print(row["name"], row["score"])

Output

text
Tom 85
Sara 92
Ali 70

Writing a CSV File

Use csv.writer().

python
import csv

with open("products.csv", "w", newline="") as file:
    writer = csv.writer(file)

    writer.writerow(["name", "price"])
    writer.writerow(["Book", 10])
    writer.writerow(["Pen", 2])

File Content

text
name,price
Book,10
Pen,2

Appending Rows

python
import csv

with open("products.csv", "a", newline="") as file:
    writer = csv.writer(file)
    writer.writerow(["Bag", 25])

Updated File

text
name,price
Book,10
Pen,2
Bag,25

Why newline=""?

It helps avoid blank lines on some systems.

Common Beginner Errors

Forgetting import csv

The code will not work.

Wrong File Name

Check spelling carefully.

Forgetting newline=""

May create extra blank lines.

Code Along

Create cities.csv with:

text
city,country
Toronto,Canada
New York,USA

Then read and print all rows.

Mini Challenge

Build a score saver.

Steps:

  • Create scores.csv
  • Add header:
text
name,score
  • Add 3 student rows
  • Read the file
  • Print each student name and score

Expected output:

text
Tom 85
Sara 92
Ali 70

Real World Use Case

Programs use CSV files for exports, reports, backups, spreadsheets, and data sharing.

Quiz

  1. What does CSV mean?
  2. How is data separated in CSV?
  3. What does DictReader do?
  4. Why use newline="" when writing?

Assignment

Create a books.csv file with title and price columns. Add 3 books, then read and print them.

Summary

You learned how to read CSV files, skip headers, use DictReader, write rows, and store table-style data in Python.