diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8caa56f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +Agrofood_co2_emission.csv +__pycache__/ diff --git a/README.md b/README.md index 2d02f3f..d79259e 100644 --- a/README.md +++ b/README.md @@ -1 +1,17 @@ -# python-refresher \ No newline at end of file +# python-refresher + +This repository contains Python code for reading agricultural CO2 emissions data. + +### Changes ### +- Implemented "get_column()" in "my_utils.py" to read a file line-by-line and return values from a selected column +- Updated "get_column()" so that "result_column" defauls to column 1 +- Updated "print_fires.py" to print forest fire emissions for the United States +- Added "run.sh" to run "print_fires.py" + +### Running the Script ### + +Run the script with: + +bash run.sh + +The data file "Agrofood_co2_emission.csv" must be present in the repo directory but do not commit it to Git. diff --git a/my_utils.py b/my_utils.py index 923f010..ad6d803 100644 --- a/my_utils.py +++ b/my_utils.py @@ -1,2 +1,9 @@ -def get_column(file_name, query_column, query_value, result_column): - return None +def get_column(file_name, query_column, query_value, result_column=1): + result = [] + + with open(file_name, "r") as file: + for line in file: + values = line.strip().split(",") + if values[query_column] == query_value: + result.append(values[result_column]) + return result diff --git a/print_fires.py b/print_fires.py index 4992fb6..4d96c6a 100644 --- a/print_fires.py +++ b/print_fires.py @@ -1,6 +1,9 @@ +from my_utils import get_column + country='United States of America' -county_column = 1 -fires_column = 4 +country_column = 0 +fires_column = 3 file_name = 'Agrofood_co2_emission.csv' -fires = get_column() + +fires = get_column(file_name, country_column, country, result_column= fires_column) print(fires) diff --git a/run.sh b/run.sh new file mode 100644 index 0000000..769760c --- /dev/null +++ b/run.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +python print_fires.py