Skip to content

Instantly share code, notes, and snippets.

@markbrough
Created April 5, 2017 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markbrough/a532c320af8eda1d109066f067ff888e to your computer and use it in GitHub Desktop.
Save markbrough/a532c320af8eda1d109066f067ff888e to your computer and use it in GitHub Desktop.
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