Olympics Host Cities (CSV Challenge)

For this challenge we are going to work with a list of host cities of the Olympic Games since the modern Olympics began in 1896. Our list will include both Summer and Winter games. This information is stored in a CSV file called olympics.csv. We will use a Python program to access this file and extract relevant data to answer specific queries about the Olympic Games.

Comma Separated Values (CSV)

Comma-separated values (CSV) is a text file format used to save tabular data. When considering a table of data, each line of the table is called a record. A record is made of fields. Each field stores a single value for each record.

To save this structured/tabular data, a CSV file uses commas (,) to separate values/fields, and newlines to separate records: each line of the file typically represents one data record. Each record consists of the same number of fields, and these are separated by commas in the CSV file.

Note that on occasions, it is not possible to use the comma as a separator if it is likely that the comma might be used in the data being stored. In this case a less common character might be used as a separator instead of the comma. This could be a semi-colon (;) or a pipe (¦). Even though the comma is not used, we can still consider such a file to be a CSV file.

In our case, our CSV file will use one line per Olympic Games. Each line/record will consists of four fieds:

year,city,country,season

You can see below the first few lines of the olympics.csv file:

1896,Athens,Greece,Summer,
1900,Paris,France,Summer,
1904,St. Louis,United States,Summer,
1908,London,United Kingdom,Summer,
1912,Stockholm,Sweden,Summer,
1920,Antwerp,Belgium,Summer,
1924,Chamonix,France,Winter,
1924,Paris,France,Summer,
...

Python Code

We can easily read the content of a text file in Python and iterate through each line of the file:

file = open("olympics.csv","r")
for line in file:
    print(line)      
file.close()

As each line consists of 4 fields separated by a comma, we can use the split() function in Python to convert each line into a list of values. We can then extract each value one at a time:

file = open("olympics.csv","r")
for line in file:
    data = line.split(",")
    year = data[0]
    city = data[1]
    country = data[2]
    season = data[3]    
    print(city + " hosted the " + season + " olympics in " + year)
file.close()

We have completed a Python script to scan through all the lines of the CSV file to only display a list of Summer Olympic Games and ignore the Winter games.

Your Task

Update the above code to let the user:

     List all the Winter Olympic Games
     List all the Olympic Games that were hosted in the United States
     Find out how many times did London (UK) host the Olympic Games
     Let the user enter a year and find out which city hosted the Olympic Games on the year they entered.
unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Did you like this challenge?

Click on a star to rate it!

Average rating 4.2 / 5. Vote count: 26

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!

Tagged with: