Exercise 3 - Solutions

from pathlib import Path
from utils import count_items
Copy to clipboard
  1. Contstruct the path to the text file in the data directory using the pathlib module
data_dir = Path("data")
output_dir = Path("solution")

# Construct path to text file
text_file = data_dir / "cars.txt"
Copy to clipboard
  1. Read the text file
with open(text_file, "r") as file:
    # .read() : read the whole file
    # .splitlines() : splits the lines and removes the lines break token "\n"
    # [1:] : remove the first line because "model" is the header of the file
    data = file.read().splitlines()[1:]
Copy to clipboard
  1. Count the occurences of each item in the text file

Count items with imported function. To see what the function looks like click the + sign.

counts = count_items(data)
Copy to clipboard
  1. Using pathlib check if a directory with name solution exists and if not create it
if not output_dir.exists():
    output_dir.mkdir()
Copy to clipboard

Construct path to output file

out_file = output_dir / "counts.csv"
Copy to clipboard
  1. Write the counts to the file counts.csv in the solution directory in the format (first line is the header)

item, count
item_name_1, item_count_1
item_name_2, item_count_2

with open(out_file, "w") as file:
    # Write header line
    file.write("model,count\n")
    # Write model and counts line by line
    for model, num in counts.items():
        file.write("{},{}\n".format(model, num))
Copy to clipboard