Scientific Computing
Download and save the file co2-by-nation.csv to your cs100/ch5
folder. Be sure you use the same name for the file.
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.
# data from https://datahub.io/core/co2-fossil-by-nation#data
file = open("co2-by-nation.csv", "r")
# 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 = {}
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)]))
file.close()
Read through each line to understand what the program is doing and how it works.
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.
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.
Find another data set related to an area of interested! Here are a bunch - be sure to look for csv
files (C
omma S
eparated V
alues) or tsv
files (T
ab S
eparated V
alues).
Submit your working python file carbon-emissions.py
to Moodle.