This Python code shows you how to get a current list of currency values (Canadian Dollar = 1.00) via the internet.
Get An Updated Currency Listing (Python)
'''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')]
'''
TrustyTony 888 pyMod Team Colleague Featured Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.