Activity: Reading from a file - Carbon dataset

  1. Download and save the file co2-by-nation.csv to your cs100/ch5 folder. Be sure you use the same name for the file.

  2. Create a new file called carbon-emissions.py and save it to your cs100/ch5 folder. It is important that this file is in the same folder as the co2-by-nation.csv file you downloaded.

  3. Copy and paste the following code to your python file.

# column number corresponding to different data fields
IDX_YEAR = 0          # Year
IDX_COUNTRY = 1       # string - Nation
IDX_TOTAL_FUELS = 2   # Total carbon emissions from fossil fuel consumption and cement production (million    metric tons of C)
IDX_SOLID_FUEL = 3    # Carbon emissions from solid fuel consumption
IDX_LIQUID_FUEL = 4   # Carbon emissions from liquid fuel consumption
IDX_GAS_FUEL = 5      # Carbon emissions from gas fuel consumption
IDX_CEMENT = 6        # Carbon emissions from cement production
IDX_GAS_FLARING = 7   # Carbon emissions from gas flaring
IDX_PER_CAPITY = 8    # Per capita carbon emissions (metric tons of carbon; after 1949 only)
IDX_BUNKER_FUELS = 9  # Carbon emissions from bunker fuels (not included in total)

perYear = {}

# data from https://datahub.io/core/co2-fossil-by-nation#data
with open("co2-by-nation.csv", "r") as file:
    
    file.readline() # first line is header columns
    
    for line in file:
        lst = line.split(",")
           
        year = lst[IDX_YEAR]
        total = int(lst[IDX_TOTAL_FUELS])
           
        if year in perYear:
            perYear[year] += total
        else:
            perYear[year] = total
       
for yr in range(1950, 2015):
    print("{0} -- {1} million metric tons of C".format(yr, perYear[str(yr)]))       
  1. Read through each line to understand what the program is doing and how it works.

  2. Add code to print out, for each country, the total carbon emmissions as accumulated throughout the years. Format the output so that it prints out the country and the total.

  3. Add code to determine the number 1 country with the most carbon emissions. Which country is it? Save the country with the most carbon emissions in a variable called topCountry. Save the million metric tons of C emitted by that country in a variable called topEmissions. Print the results so that it is the last thing which appears in the shell/terminal when your program is run.

  4. Add code to write to an output file called c02-info.txt the text “The country with the most carbon emissions is X, which emits Y million metric tons of carbon.” Use string formatting to fill in the appropriate values for X and Y. Do not simply write in the values for X and Y by hand - use the variables you created from the previous question.

If you finish early

  1. What other information can you determine from this data? Explore a bit! For example, which year had the most emissions for liquid fuel consumption? Which country has the most carbon emissions from cement production? What other questions can you answer? Print your results.

  2. Find another data set related to an area of interested! Here are a bunch - be sure to look for csv files (Comma Separated Values) or tsv files (Tab Separated Values).

How to submit

Submit your working python file carbon-emissions.py to Moodle.