In this part, we move from setup to real dataset processing with pure Python.
In this tutorial, you will:
- download a dataset
- read CSV data using Python's built-in
csvmodule - practice basic data-processing tasks
- reason about time and space complexity
Before starting:
-
Open the
session1folder in Visual Studio Code. -
Activate your virtual environment.
-
Confirm dependencies are installed:
pip install -r requirements.txtFor this tutorial, the hf command requires huggingface_hub. Make sure your requirements.txt includes:
huggingface_hub- Create (or open) your exercise file:
session1/solutions/exercise-01-03.pyIf solutions/ does not exist yet, create it first (see Part 1).
csvmodule: built-in Python module for reading/writing CSV files.csv.reader(file): returns an iterator over rows.- Each row is a
listof strings. - Header row: first row with column names.
break: stops search when the first matching row is found.Hugging Face: a platform for hosting datasets and models.hfCLI: command-line tool fromhuggingface_hubused to download datasets.hf download: downloads a specific file from a dataset or model repository.
Dataset:
Download the dataset file using the following command in terminal (Mac or Windows):
hf download Birkbeck/movies movies.csv --repo-type dataset --local-dir .Expected result: movies.csv appears in your current folder.
Let's read the file row by row. This prints each CSV row as a Python list.
Run this script from the session1 folder (where movies.csv was downloaded). If you run from the bda root, use open("session1/movies.csv", "r").
File: session1/solutions/exercise-01-03.py
import csv
with open("movies.csv", "r", newline="", encoding="utf-8") as file:
reader = csv.reader(file)
for row in reader:
print(row)Run the script using:
python solutions/exercise-01-03.py
Example output:
['movie_id', 'title', 'year', ...]
['1', 'Movie 1', '2020', ...]
['2', 'Movie 2', '1994', '144', ...]Tip
What are the time and space complexities of this operation?
Show answer
Time: O(n)
Space: O(1)
Check what reader is:
File: session1/solutions/exercise-01-03.py
import csv
with open("movies.csv", "r", newline="", encoding="utf-8") as file:
reader = csv.reader(file)
print(reader)Example output:
<_csv.reader object at 0x...>Print the genres column (index 4):
File: session1/solutions/exercise-01-03.py
import csv
with open("movies.csv", "r", newline="", encoding="utf-8") as file:
reader = csv.reader(file)
for row in reader:
print(row[4])This script assumes all rows have at least 5 columns. To avoid errors, add a check such as if len(row) > 4: before accessing the column.
Tip
What are the time and space complexities of the above script?
Show answer
Time: O(n)
Space: O(1)
Write all answers in your solutions/ folder:
session1/solutions/exercise-01-03.pyTasks:
- Print only the first row (header).
- Print the first 5 rows only.
- Find and print the first movie where
genrescontainsAction, then stop. - State one benefit and one limitation of using raw
csv.reader. - What are the time and space complexities of your script(s)?
Use this file:
session1/solutions/exercise-01-03.pyUse the dataset here. You will need to download the data using the appropriate hf command.
There is a data issue (e.g. missing or malformed row). Identify:
- which row is problematic
- which column is affected
- what the issue is
This dataset may also contain a file named movies.csv. If you download it into the same folder, you can overwrite your original movies.csv.
Note
Use enumerate(reader) to track row numbers and detect malformed rows.
Show code
for i, row in enumerate(reader):
if len(row) != expected_columns:
print(f"Issue at row {i}: {row}")Set expected_columns based on the header length.
Complete the following quiz.
quizmd quizzes/python-loops-and-indexing-quiz.mdNow complete the imposter quiz:
Note
The next quiz is an imposter quiz 🤥.
You need to identify:
- the correct answer and
- the answer that looks correct but is actually wrong. Read the quiz instructions before you start.
Example:
What is the index of 20 in [10, 20, 30]?
Correct answer: 1
Imposter answer: 2 (common mistake due to misunderstanding 0-based indexing)
quizmd quizzes/python-session-01-imposter-quiz.md