opentraveldata/geobases — pull request #20 by tadhgpearson (2014-08-15 20:55:29+00:00)
Title: Include currency in ori_por_public.csv

Helpful to determine which officeID should be used for a given origin when making faring queries.

Currency data taken from https://raw.githubusercontent.com/datasets/country-codes/master/data/country-codes.csv

Code for currency merge below:

import csv

def add_currency_to_points(por_reader, currencies):
por_with_currency = []
for point in por_reader:
country = point['country_code']

if country in currencies:
currency = currencies[country]
point['currency_code'] = currency
else:
print "Could not parse " + point[
'iata_code'] + " Country code is " + country + " Currency code not found"

por_with_currency.append(point)

return por_with_currency

def read_currencies(currency_file_path):
currency_file = open(currency_file_path, 'rb')
currency_reader = csv.DictReader(currency_file)
currencies = {}
for c in currency_reader:
currency_code = c['currency_alphabetic_code']
country_code = c['ISO3166-1-Alpha-2']
currencies[country_code] = currency_code
currency_file.close()
return currencies

currency_file_path = 'country-codes.csv'
locations_file_path = 'ori_por_public.csv'
output_file_path = 'ori_por_public_out.csv'

locations_file = open(locations_file_path, 'rb')
por_reader = csv.DictReader(locations_file, delimiter='^', )
fieldnames = por_reader.fieldnames

currencies = read_currencies(currency_file_path)

por_with_currency = add_currency_to_points(por_reader, currencies)

locations_file.close()

fieldnames.append('currency_code')
output_file = open(output_file_path, 'wb')
out = csv.DictWriter(output_file, delimiter='^', fieldnames=fieldnames)
out.writeheader()
out.writerows(por_with_currency)
output_file.close()

Signed-off-by: tpearson