Exercise 3 - Solutions

from pathlib import Path
from utils import count_items
  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"
  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:]
  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.

def count_items(x):
    """
    Count occurence of unique items of list

    Counts items in the list and returns counts as a dict

    Parameters
    ----------
    x : list
        List of items to count

    Returns
    -------
    dict
        Items are keys, counts are values

    """
    # init an empty dictionary
    counts = {}
    # loop through the list of items (lines)
    for item in x:
        # check if it already is in the dictionary
        # if it is increase count by one if not add it
        if item in counts.keys():
            counts[item] += 1
        else:
            counts[item] = 1

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

Construct path to output file

out_file = output_dir / "counts.csv"
  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))