Exercise 3 - Solutions
from pathlib import Path
from utils import count_items
- 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"
- 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:]
- 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)
- Using
pathlib
check if a directory with namesolution
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"
- Write the counts to the file
counts.csv
in thesolution
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))