Mathew K Analytics

Lesson 5 · Data analytics zero to hero

Read & Write Files in Python (CSV & TXT) | Data Analytics #5

Video five of the 30-part series, and the final foundations video: reading and writing text, CSV, and JSON files in plain Python. We'll use a real dataset…

What you'll learn

Datasets used in this lesson

Save these next to the notebook. In Google Colab, upload them with the 📁 icon on the left first.

📓 Full notebook

Download .ipynb

Data Analytics Zero to Hero, Video 5: Working with Files#

  • Video five of the 30-part series, and the final foundations video: reading and writing text, CSV, and JSON files in plain Python.
  • We'll use a real dataset for the first time: mtcars, the classic set of 1974 Motor Trend car road tests.
  • Let's jump straight in.

Before You Start#

  • Open a new Jupyter Notebook in VS Code and select your Python interpreter as the kernel.
  • Place mtcars.csv in the same folder as this notebook.

Part 1: Writing and Reading Text Files#

with open('notes.txt', 'w') as f:
    f.write('Data Analytics Zero to Hero\n')
    f.write('Video 5: Working with Files\n')
print('File written.')
File written.
with open('notes.txt', 'r') as f:
    contents = f.read()
print(contents)
Data Analytics Zero to Hero
Video 5: Working with Files

with open('notes.txt', 'r') as f:
    for line in f:
        print(line.strip())
Data Analytics Zero to Hero
Video 5: Working with Files
with open('notes.txt', 'a') as f:
    f.write('Appended line.\n')
with open('notes.txt', 'r') as f:
    print(f.read())
Data Analytics Zero to Hero
Video 5: Working with Files
Appended line.

Part 2: Reading a Real CSV File#

import csv

with open('mtcars.csv', 'r') as f:
    reader = csv.reader(f)
    header = next(reader)
    print(header)
['model', 'mpg', 'cyl', 'disp', 'hp', 'drat', 'wt', 'qsec', 'vs', 'am', 'gear', 'carb']
with open('mtcars.csv', 'r') as f:
    reader = csv.reader(f)
    header = next(reader)
    rows = list(reader)
print(len(rows))
print(rows[0])
32
['Mazda RX4', '21', '6', '160', '110', '3.9', '2.62', '16.46', '0', '1', '4', '4']

DictReader for Named Columns#

with open('mtcars.csv', 'r') as f:
    reader = csv.DictReader(f)
    cars = list(reader)
print(cars[0])
print(cars[0]['model'])
print(cars[0]['mpg'])
{'model': 'Mazda RX4', 'mpg': '21', 'cyl': '6', 'disp': '160', 'hp': '110', 'drat': '3.9', 'wt': '2.62', 'qsec': '16.46', 'vs': '0', 'am': '1', 'gear': '4', 'carb': '4'}
Mazda RX4
21
total_mpg = 0
for car in cars:
    total_mpg = total_mpg + float(car['mpg'])
average_mpg = total_mpg / len(cars)
print(f'Average MPG across all 32 cars: {average_mpg:.2f}')
Average MPG across all 32 cars: 20.09

Part 3: Writing a CSV File#

efficient_cars = [car for car in cars if float(car['mpg']) > 25]
print(len(efficient_cars))

with open('efficient_cars.csv', 'w', newline='') as f:
    writer = csv.DictWriter(f, fieldnames=['model', 'mpg', 'cyl'])
    writer.writeheader()
    for car in efficient_cars:
        writer.writerow({'model': car['model'], 'mpg': car['mpg'], 'cyl': car['cyl']})
print('Saved efficient_cars.csv')
6
Saved efficient_cars.csv

Part 4: Working with JSON#

import json

top_3_efficient = sorted(efficient_cars, key=lambda c: float(c['mpg']), reverse=True)[:3]
with open('top_cars.json', 'w') as f:
    json.dump(top_3_efficient, f, indent=2)
print('Saved top_cars.json')
Saved top_cars.json
with open('top_cars.json', 'r') as f:
    loaded = json.load(f)
print(loaded)
print(type(loaded))
[{'model': 'Toyota Corolla', 'mpg': '33.9', 'cyl': '4', 'disp': '71.1', 'hp': '65', 'drat': '4.22', 'wt': '1.835', 'qsec': '19.9', 'vs': '1', 'am': '1', 'gear': '4', 'carb': '1'}, {'model': 'Fiat 128', 'mpg': '32.4', 'cyl': '4', 'disp': '78.7', 'hp': '66', 'drat': '4.08', 'wt': '2.2', 'qsec': '19.47', 'vs': '1', 'am': '1', 'gear': '4', 'carb': '1'}, {'model': 'Honda Civic', 'mpg': '30.4', 'cyl': '4', 'disp': '75.7', 'hp': '52', 'drat': '4.93', 'wt': '1.615', 'qsec': '18.52', 'vs': '1', 'am': '1', 'gear': '4', 'carb': '2'}]
<class 'list'>
print(json.dumps({'model': 'Fiat 128', 'mpg': 32.4}))
{"model": "Fiat 128", "mpg": 32.4}

Wrap-Up: What You Learned#

  • Reading and writing plain text files with open(), in read, write, and append modes.
  • Reading a real CSV file with csv.reader and csv.DictReader.
  • Writing a CSV file with csv.DictWriter.
  • Reading and writing JSON with json.load, json.dump, and json.dumps.
  • All of this on a real dataset: 1974 Motor Trend car road tests.
  • That's the full foundations block done: variables through file handling. Video six starts the next block with NumPy, working with real numeric data at scale. Subscribe so it lands automatically see you there.

Found this useful?

All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.