Created
April 5, 2017 14:14
-
-
Save markbrough/a532c320af8eda1d109066f067ff888e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unicodecsv | |
import exchangerates | |
from exchangerates import UnknownCurrencyException | |
out_f = open("uganda/uganda-activity-clean.csv", "w") | |
class InvalidDataException(Exception): | |
pass | |
def get_any_date(row): | |
if row.get("end-actual"): | |
return row.get("end-actual") | |
if row.get("end-planned"): | |
return row.get("end-planned") | |
if row.get("start-actual"): | |
return row.get("start-actual") | |
if row.get("start-planned"): | |
return row.get("start-planned") | |
raise InvalidDataException("No date found for activity {}".format( | |
row["iati-identifier"].strip()) | |
) | |
def get_any_currency(row): | |
if row.get("default-currency"): | |
return row.get("default-currency") | |
if row.get("currency"): | |
return row.get("currency") | |
raise InvalidDataException("No currency found for activity {}".format( | |
row["iati-identifier"].strip()) | |
) | |
with open("uganda/uganda-activity.csv", "r") as csv_f: | |
csv = unicodecsv.DictReader(csv_f) | |
out_csv = unicodecsv.DictWriter(out_f, fieldnames=csv.fieldnames+[ | |
"total-Disbursement_USD","total-Commitment_USD" | |
]) | |
out_csv.writeheader() | |
rates_data = exchangerates.rates_keys() | |
for row in csv: | |
try: | |
exchange_rate = exchangerates.closest_exchange_rate(rates_data[0], rates_data[1], | |
[(get_any_currency(row), exchangerates.make_date_from_iso( | |
get_any_date(row) | |
))]) | |
row["total-Commitment_USD"] = float(row["total-Commitment"])*float(exchange_rate[1]) | |
row["total-Disbursement_USD"] = float(row["total-Disbursement"])*float(exchange_rate[1]) | |
except UnknownCurrencyException, e: | |
print "WARNING: {}".format(e) | |
continue | |
except InvalidDataException, e: | |
print "WARNING: {}".format(e) | |
continue | |
except ValueError, e: | |
print "WARNING: {} for activity {}".format(e, row["iati-identifier"]) | |
out_csv.writerow(row) | |
out_f.close() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment