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…
- CourseData analytics zero to hero
- Lesson5 of 30
- Video13 min
- FormatJupyter notebook · 12 code cells
- Data1 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.
- mtcars.csv1.7 KB
📓 Full notebook
Download .ipynbData 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.')
with open('notes.txt', 'r') as f:
contents = f.read()
print(contents)
with open('notes.txt', 'r') as f:
for line in f:
print(line.strip())
with open('notes.txt', 'a') as f:
f.write('Appended line.\n')
with open('notes.txt', 'r') as f:
print(f.read())
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)
with open('mtcars.csv', 'r') as f:
reader = csv.reader(f)
header = next(reader)
rows = list(reader)
print(len(rows))
print(rows[0])
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'])
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}')
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')
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')
with open('top_cars.json', 'r') as f:
loaded = json.load(f)
print(loaded)
print(type(loaded))
print(json.dumps({'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.



