Monday, June 14, 2021

Python Practice Problems: Parsing CSV Files

Are you a developer looking for some practice with comma-separated values (CSV) files before an upcoming interview? This tutorial will lead you through a series of Python CSV practice problems to help you get ready.

This tutorial is aimed at intermediate Python developers. It assumes a basic knowledge of Python and working with CSV files. Like other practice problem tutorials, each of the problems listed here shows the problem description. You’ll see the problem statement first and then have a chance to develop your own solution.

In this tutorial, you’ll explore:

  • Writing code for working with CSV files
  • Doing test-driven development with pytest
  • Discussing your solutions and possible enhancements
  • The trade-offs between the built-in CSV module and pandas

You can get skeleton code with failing unit tests for each of the problems you’ll see in this tutorial by clicking the link below:

Python CSV Parsing: Football Scores

Your first problem deals with English Premier League team standings. You don’t need any special football knowledge to solve this, just Python!

As you work through the problem, try to write more unit tests for each bit of functionality and then write the functionality to make the tests pass. This is known as test-driven development, and it can be a great way to show off not only your coding but also your testing chops!

Problem Description

For this round of the problem, stick to the standard library csv module. You’ll get another shot at it using pandas later. Here’s your first problem:

Find the Minimum Goal Differential

Write a program that takes a filename on the command line and processes the contents of a CSV file. The contents will be the end-of-season football standings for the English Premier League. Your program should determine which team had the smallest goal differential that season.

The first line of the CSV file will be column headers, with each subsequent line showing the data for one team:

Team,Games,Wins,Losses,Draws,Goals For,Goals Against
Arsenal,38,26,9,3,79,36

The columns labeled Goals For and Goals Against contain the total number of goals scored for and against each team in that season. (So Arsenal scored 79 goals and had 36 goals scored against them.)

Write a program to read the file, then print the name of the team with the smallest difference in Goals For and Goals Against. Create unit tests with pytest to test your program.

There is a single unit test supplied in the skeleton code that tests the problem statement that you’ll see later. You can add more as you write your solution. There are also two pytest fixtures given:

# test_football_v1.py
import pytest
import football_v1 as fb

@pytest.fixture
def mock_csv_data():
    return [
        "Team,Games,Wins,Losses,Draws,Goals For,Goals Against",
        "Liverpool FC, 38, 32, 3, 3, 85, 33",
        "Norwich City FC, 38, 5, 27, 6, 26, 75",
    ]

@pytest.fixture
def mock_csv_file(tmp_path, mock_csv_data):
    datafile = tmp_path / "football.csv"
    datafile.write_text("\n".join(mock_csv_data))
    return str(datafile)

Read the full article at https://realpython.com/python-interview-problem-parsing-csv-files/ »


[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]



from Real Python
read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...