'''currency1.py
open a currency rate URL for reading
tested with Python27 and Python32/33
'''

try:
    # Python2
    from urllib2 import urlopen
except ImportError:
    # Python3
    from urllib.request import urlopen

url = "http://www.bankofcanada.ca/en/markets/csv/exchange_eng.csv"

fp = urlopen(url)

currency_list = [('Canadian dollar ', ' 1.0000')]
for ix, line in enumerate(fp):
    # remove trailing new line
    line = line.rstrip()
    if line.startswith(b'#'):
        continue
    if line.startswith(b"Date"):
        date = line.split(b",")[-1]
    #print(ix)  # test
    if ix == 7 or ix > 10:
        line_list = line.split(b",")
        currency_list.append((line_list[0], line_list[-1]))


print("year-month-day = %s" % date)
import pprint
pprint.pprint(currency_list)

'''Python27 result list of (country, rate vs. Canadian Dollar) tuples ...
year-month-day =  2012-10-09
[('Canadian dollar ', ' 1.0000'),
 ('U.S. dollar ', ' 0.9788'),
 ('Argentine peso', ' 0.2078'),
 ('Australian dollar', ' 0.9970'),
 ('Bahamian dollar', ' 0.9788'),
 ('Brazilian real', ' 0.4808'),
 ('CFA franc (African Financial Community)', ' 0.001921'),
 ('CFP franc (Pacific Financial Community)', ' 0.01056'),
 ('Chilean peso', ' 0.002061'),
 ('Chinese renminbi ', ' 0.1557'),
 ('Colombian peso', ' 0.000544'),
 ('Croatian kuna', ' 0.1689'),
 ('Czech Republic koruna', ' 0.05038'),
 ('Danish krone', ' 0.1690'),
 ('East Caribbean dollar', ' 0.3680'),
 ('European Euro ', ' 1.2601'),
 ('Fiji dollar', ' 0.5511'),
 ('Ghanaian cedi ', ' 0.5191'),
 ('Guatemalan quetzal', ' 0.1224'),
 ('Honduran lempira', ' 0.04971'),
 ('Hong Kong dollar ', ' 0.126259'),
 ('Hungarian forint', ' 0.004445'),
 ('Icelandic krona', ' 0.007960'),
 ('Indian rupee', ' 0.01856'),
 ('Indonesian rupiah', ' 0.000102'),
 ('Israeli new shekel', ' 0.2528'),
 ('Jamaican dollar', ' 0.01092'),
 ('Japanese yen ', ' 0.01251'),
 ('Malaysian ringgit', ' 0.3188'),
 ('Mexican peso ', ' 0.07593'),
 ('Moroccan dirham', ' 0.1136'),
 ('Myanmar (Burma) kyat', ' 0.00114'),
 ('Neth. Antilles florin', ' 0.5499'),
 ('New Zealand dollar', ' 0.7994'),
 ('Norwegian krone', ' 0.1706'),
 ('Pakistan rupee', ' 0.01024'),
 ('Panamanian balboa', ' 0.9788'),
 ('Peruvian new sol', ' 0.3783'),
 ('Philippine peso', ' 0.02361'),
 ('Polish zloty', ' 0.3089'),
 ('Romanian new leu', ' 0.2753'),
 ('Russian rouble', ' 0.03135'),
 ('Serbian dinar', ' 0.01099'),
 ('Singapore dollar', ' 0.7956'),
 ('South African rand', ' 0.1118'),
 ('South Korean won', ' 0.000881'),
 ('Sri Lanka rupee', ' 0.007628'),
 ('Swedish krona ', ' 0.1462'),
 ('Swiss franc', ' 1.0412'),
 ('Taiwanese new dollar', ' 0.03341'),
 ('Thai baht', ' 0.03190'),
 ('Trinidad and Tobago dollar', ' 0.1529'),
 ('Tunisian dinar', ' 0.6215'),
 ('New Turkish lira', ' 0.5376'),
 ('U.A.E. dirham', ' 0.2665'),
 ('U.K. pound sterling ', ' 1.5647'),
 ('Venezuelan bolivar fuerte', ' 0.2279'),
 ('Vietnamese dong', ' 0.000047')]
'''
2
Contributors
1
Reply
5
Views
4 Years
Discussion Span
Last Post by pyTony
1

Some work on it to dictionaries and changing the base currency by function updating the mutable dict. Also changed the rates to floats to be more usefull.

'''currency.py
open a currency rate URL for reading
tested with Python27 and Python32/33
based on vegaseat's code
'''

import pprint
try:
    # Python2
    from urllib2 import urlopen
except ImportError:
    # Python3
    from urllib.request import urlopen

def get_currencies():
    url = "http://www.bankofcanada.ca/en/markets/csv/exchange_eng.csv"

    fp = urlopen(url)

    currencies = {b'Canadian dollar': 1.00}
    for ix, line in enumerate(fp):
        # remove trailing new line
        line = line.rstrip()
        if line.startswith(b'#'):
            continue
        if line.startswith(b"Date"):
            date = line.split(b",")[-1]
        #print(ix)  # test
        if ix == 7 or ix > 10:
            line_list = line.split(b",")
            currencies[bytes(line_list[0].strip())] = float(line_list[-1])
    return date, currencies

def base_currency(base, currencies):
    b = currencies[base]
    currencies.update(dict((currency, value/b) for currency,value in currencies.items()))

date, currencies = get_currencies()
print("year-month-day = %s" % date)
pprint.pprint(currencies)
base = b'U.S. dollar'
base_currency(base, currencies)

print('\nBased on %s' % base)
pprint.pprint(currencies)

""" Output:
year-month-day = b' 2012-10-09'
{b'Argentine peso': 0.2078,
 b'Australian dollar': 0.997,
 b'Bahamian dollar': 0.9788,
 b'Brazilian real': 0.4808,
 b'CFA franc (African Financial Community)': 0.001921,
 b'CFP franc (Pacific Financial Community)': 0.01056,
 b'Canadian dollar': 1.0,
 b'Chilean peso': 0.002061,
 b'Chinese renminbi': 0.1557,
 b'Colombian peso': 0.000544,
 b'Croatian kuna': 0.1689,
 b'Czech Republic koruna': 0.05038,
 b'Danish krone': 0.169,
 b'East Caribbean dollar': 0.368,
 b'European Euro': 1.2601,
 b'Fiji dollar': 0.5511,
 b'Ghanaian cedi': 0.5191,
 b'Guatemalan quetzal': 0.1224,
 b'Honduran lempira': 0.04971,
 b'Hong Kong dollar': 0.126259,
 b'Hungarian forint': 0.004445,
 b'Icelandic krona': 0.00796,
 b'Indian rupee': 0.01856,
 b'Indonesian rupiah': 0.000102,
 b'Israeli new shekel': 0.2528,
 b'Jamaican dollar': 0.01092,
 b'Japanese yen': 0.01251,
 b'Malaysian ringgit': 0.3188,
 b'Mexican peso': 0.07593,
 b'Moroccan dirham': 0.1136,
 b'Myanmar (Burma) kyat': 0.00114,
 b'Neth. Antilles florin': 0.5499,
 b'New Turkish lira': 0.5376,
 b'New Zealand dollar': 0.7994,
 b'Norwegian krone': 0.1706,
 b'Pakistan rupee': 0.01024,
 b'Panamanian balboa': 0.9788,
 b'Peruvian new sol': 0.3783,
 b'Philippine peso': 0.02361,
 b'Polish zloty': 0.3089,
 b'Romanian new leu': 0.2753,
 b'Russian rouble': 0.03135,
 b'Serbian dinar': 0.01099,
 b'Singapore dollar': 0.7956,
 b'South African rand': 0.1118,
 b'South Korean won': 0.000881,
 b'Sri Lanka rupee': 0.007628,
 b'Swedish krona': 0.1462,
 b'Swiss franc': 1.0412,
 b'Taiwanese new dollar': 0.03341,
 b'Thai baht': 0.0319,
 b'Trinidad and Tobago dollar': 0.1529,
 b'Tunisian dinar': 0.6215,
 b'U.A.E. dirham': 0.2665,
 b'U.K. pound sterling': 1.5647,
 b'U.S. dollar': 0.9788,
 b'Venezuelan bolivar fuerte': 0.2279,
 b'Vietnamese dong': 4.7e-05}

Based on b'U.S. dollar'
{b'Argentine peso': 0.21230077646097262,
 b'Australian dollar': 1.018594196975889,
 b'Bahamian dollar': 1.0,
 b'Brazilian real': 0.4912137310993053,
 b'CFA franc (African Financial Community)': 0.0019626072742133225,
 b'CFP franc (Pacific Financial Community)': 0.010788720882713527,
 b'Canadian dollar': 1.021659174499387,
 b'Chilean peso': 0.0021056395586432366,
 b'Chinese renminbi': 0.15907233346955457,
 b'Colombian peso': 0.0005557825909276665,
 b'Croatian kuna': 0.17255823457294645,
 b'Czech Republic koruna': 0.05147118921127912,
 b'Danish krone': 0.1726604004903964,
 b'East Caribbean dollar': 0.3759705762157744,
 b'European Euro': 1.2873927257866775,
 b'Fiji dollar': 0.5630363710666122,
 b'Ghanaian cedi': 0.5303432774826318,
 b'Guatemalan quetzal': 0.12505108295872497,
 b'Honduran lempira': 0.05078667756436452,
 b'Hong Kong dollar': 0.12899366571311813,
 b'Hungarian forint': 0.004541275030649775,
 b'Icelandic krona': 0.008132407029015121,
 b'Indian rupee': 0.01896199427870862,
 b'Indonesian rupiah': 0.00010420923579893747,
 b'Israeli new shekel': 0.25827543931344504,
 b'Jamaican dollar': 0.011156518185533305,
 b'Japanese yen': 0.012780956272987332,
 b'Malaysian ringgit': 0.3257049448304046,
 b'Mexican peso': 0.07757458111973846,
 b'Moroccan dirham': 0.11606048222313037,
 b'Myanmar (Burma) kyat': 0.001164691458929301,
 b'Neth. Antilles florin': 0.561810380057213,
 b'New Turkish lira': 0.5492439722108704,
 b'New Zealand dollar': 0.8167143440948099,
 b'Norwegian krone': 0.17429505516959543,
 b'Pakistan rupee': 0.010461789946873724,
 b'Panamanian balboa': 1.0,
 b'Peruvian new sol': 0.3864936657131181,
 b'Philippine peso': 0.024121373109930525,
 b'Polish zloty': 0.31559051900286067,
 b'Romanian new leu': 0.28126277073968126,
 b'Russian rouble': 0.03202901512055579,
 b'Serbian dinar': 0.011228034327748262,
 b'Singapore dollar': 0.8128320392317123,
 b'South African rand': 0.11422149570903146,
 b'South Korean won': 0.0009000817327339599,
 b'Sri Lanka rupee': 0.007793216183081324,
 b'Swedish krona': 0.1493665713118104,
 b'Swiss franc': 1.0637515324887616,
 b'Taiwanese new dollar': 0.03413363302002452,
 b'Thai baht': 0.03259092766653044,
 b'Trinidad and Tobago dollar': 0.1562116877809563,
 b'Tunisian dinar': 0.6349611769513691,
 b'U.A.E. dirham': 0.27227217000408666,
 b'U.K. pound sterling': 1.5985901103391909,
 b'U.S. dollar': 1.0,
 b'Venezuelan bolivar fuerte': 0.2328361258684103,
 b'Vietnamese dong': 4.8017981201471186e-05}
 """
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.