Blog Post by Murat Yaşar


Posted on Tuesday, November 08, 2016 11:47:17 AM and it has been read 808 times since then.


Downloading And Parsing XML File Using Python & Tkinter

Hello once again,

 

After a long time break, I am here to share some python code. These are very basic, beginner type of coding.

I am trying to learn Python by myself and try to write some code in order to learn it as I usually do while learning a new language.

Python is also one of the most chosen programming languages for writing IoT (Internet of Things) projects. I am really curious about this IoT bandwagon.


If you have time, I would suggest you to take a look at the following urls. They are talking about future of IoT. It is tempting.

https://www.ericsson.com/news/2016987

http://www.forbes.com/sites/louiscolumbus/2016/07/09/internet-of-things-on-pace-to-replace-mobile-phones-as-most-connected-device-in-2018

http://mobilefuture.org/issues/internet-of-things/


The sample code I am sharing here has no relevance with IoT concept but the language itself has.

This code will parse an xml file which is going to be downloaded from the internet. TCMB (Central Bank of Turkey) announces indicative exchange rates daily here in Turkey. http://tcmb.gov.tr/ is the web site address of TCMB. I simply try to get today's exchange rates from the web site as xml data and display the result to the end user.

 

I used the following platforms for writing this sample code.

Windows 10 (64-bit) operating system. (https://www.microsoft.com/en-us/windows/windows-10-specifications)

Visual Studio Code as editor. (https://code.visualstudio.com/Download)

Python 3.5.2 (https://www.python.org/downloads/)

I used PyCharm (https://www.jetbrains.com/pycharm/) but using Visual Studio Code made me feel more comfortable while writing and debugging the code.

 

I used extension from Don Jayamanne for Python in Visual Studio Code. I will not explain how to install extensions in Visual Studio Code. You can find many and very well explained resources on the internet by googling or binging.

Since I use different versions of Visual Studio most of the time, intellisense (code completion) is the biggest lack of capability in editors for Python language.

I mean there is intellisense but not the way I accustomed to in Visual Studio for writing C# code. I need to find out by reading documentation or internet for correct syntax.

I hope it can be fixed in the future.

 

The code is self explanatory. I am going to explain import statements a little bit. It is not possible to write here every possible aspect of these modules.

You will find many resources on the internet if you want to know in depth.


import requests

Requests is an elegant and simple HTTP library for Python. Requests allow you to send HTTP/1.1 requests.
In this sample I installed request using pip.

pip install requests

When you make a request you will get response object. You can get more information about request at the following url.

http://docs.python-requests.org/en/master/user/quickstart/


import xml.etree.ElementTree as ET

The following urls contains the information about this import statement. What it is and why we need it are being explained on these urls.

https://docs.python.org/3/library/xml.etree.elementtree.html

http://eli.thegreenplace.net/2012/03/15/processing-xml-in-python-with-elementtree


import tkinter as tk

The following urls contains the information about this import statement. What it is and why we need it are being explained on these urls.

https://docs.python.org/3/library/tkinter.html

https://www.youtube.com/playlist?list=PL6gx4Cwl9DGBwibXFtPtflztSNPGuIB_d

http://www.datadependence.com/2016/04/how-to-build-gui-in-python-3/


After importing the necessary modules, I wrote the below code. I downloaded the xml data and converted it to an xml element hierarchy called ElementTree. Then created labels and put the parsed data. You can see the downloaded xml data below.


<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="isokur.xsl"?>
<Tarih_Date Tarih="07.11.2016" Date="11/07/2016"  Bulten_No="2016/210" >
	<Currency CrossOrder="0" Kod="USD" CurrencyCode="USD">
			<Unit>1</Unit>
			<Isim>ABD DOLARI</Isim>
			<CurrencyName>US DOLLAR</CurrencyName>
			<ForexBuying>3.1532</ForexBuying>
			<ForexSelling>3.1589</ForexSelling>
			<BanknoteBuying>3.1510</BanknoteBuying>
			<BanknoteSelling>3.1637</BanknoteSelling>
			<CrossRateUSD/>
			<CrossRateOther/>
		
	</Currency>
	<Currency CrossOrder="1" Kod="AUD" CurrencyCode="AUD">
			<Unit>1</Unit>
			<Isim>AVUSTRALYA DOLARI</Isim>
			<CurrencyName>AUSTRALIAN DOLLAR</CurrencyName>
			<ForexBuying>2.4154</ForexBuying>
			<ForexSelling>2.4311</ForexSelling>
			<BanknoteBuying>2.4042</BanknoteBuying>
			<BanknoteSelling>2.4457</BanknoteSelling>
				<CrossRateUSD>1.3024</CrossRateUSD>
				<CrossRateOther/>
		
	</Currency>
	<Currency CrossOrder="2" Kod="DKK" CurrencyCode="DKK">
			<Unit>1</Unit>
			<Isim>DANİMARKA KRONU</Isim>
			<CurrencyName>DANISH KRONE</CurrencyName>
			<ForexBuying>0.46804</ForexBuying>
			<ForexSelling>0.47034</ForexSelling>
			<BanknoteBuying>0.46772</BanknoteBuying>
			<BanknoteSelling>0.47142</BanknoteSelling>
				<CrossRateUSD>6.7266</CrossRateUSD>
				<CrossRateOther/>
		
	</Currency>
	<Currency CrossOrder="9" Kod="EUR" CurrencyCode="EUR">
			<Unit>1</Unit>
			<Isim>EURO</Isim>
			<CurrencyName>EURO</CurrencyName>
			<ForexBuying>3.4878</ForexBuying>
			<ForexSelling>3.4941</ForexSelling>
			<BanknoteBuying>3.4854</BanknoteBuying>
			<BanknoteSelling>3.4994</BanknoteSelling>
				<CrossRateUSD/>
				<CrossRateOther>1.1061</CrossRateOther>
		
	</Currency>
	<Currency CrossOrder="10" Kod="GBP" CurrencyCode="GBP">
			<Unit>1</Unit>
			<Isim>İNGİLİZ STERLİNİ</Isim>
			<CurrencyName>POUND STERLING</CurrencyName>
			<ForexBuying>3.9062</ForexBuying>
			<ForexSelling>3.9266</ForexSelling>
			<BanknoteBuying>3.9035</BanknoteBuying>
			<BanknoteSelling>3.9325</BanknoteSelling>
				<CrossRateUSD/>
				<CrossRateOther>1.2409</CrossRateOther>
		
	</Currency>
	<Currency CrossOrder="3" Kod="CHF" CurrencyCode="CHF">
			<Unit>1</Unit>
			<Isim>İSVİÇRE FRANGI</Isim>
			<CurrencyName>SWISS FRANK</CurrencyName>
			<ForexBuying>3.2189</ForexBuying>
			<ForexSelling>3.2396</ForexSelling>
			<BanknoteBuying>3.2141</BanknoteBuying>
			<BanknoteSelling>3.2444</BanknoteSelling>
				<CrossRateUSD>0.9774</CrossRateUSD>
				<CrossRateOther/>
		
	</Currency>
	<Currency CrossOrder="4" Kod="SEK" CurrencyCode="SEK">
			<Unit>1</Unit>
			<Isim>İSVEÇ KRONU</Isim>
			<CurrencyName>SWEDISH KRONA</CurrencyName>
			<ForexBuying>0.34867</ForexBuying>
			<ForexSelling>0.35228</ForexSelling>
			<BanknoteBuying>0.34842</BanknoteBuying>
			<BanknoteSelling>0.35309</BanknoteSelling>
				<CrossRateUSD>9.0052</CrossRateUSD>
				<CrossRateOther/>
		
	</Currency>
	<Currency CrossOrder="6" Kod="CAD" CurrencyCode="CAD">
			<Unit>1</Unit>
			<Isim>KANADA DOLARI</Isim>
			<CurrencyName>CANADIAN DOLLAR</CurrencyName>
			<ForexBuying>2.3508</ForexBuying>
			<ForexSelling>2.3614</ForexSelling>
			<BanknoteBuying>2.3421</BanknoteBuying>
			<BanknoteSelling>2.3704</BanknoteSelling>
				<CrossRateUSD>1.3395</CrossRateUSD>
				<CrossRateOther/>
		
	</Currency>
	<Currency CrossOrder="11" Kod="KWD" CurrencyCode="KWD">
			<Unit>1</Unit>
			<Isim>KUVEYT DİNARI</Isim>
			<CurrencyName>KUWAITI DINAR</CurrencyName>
			<ForexBuying>10.3605</ForexBuying>
			<ForexSelling>10.4961</ForexSelling>
			<BanknoteBuying>10.2051</BanknoteBuying>
			<BanknoteSelling>10.6535</BanknoteSelling>
				<CrossRateUSD/>
				<CrossRateOther>3.3042</CrossRateOther>
		
	</Currency>
	<Currency CrossOrder="7" Kod="NOK" CurrencyCode="NOK">
			<Unit>1</Unit>
			<Isim>NORVEÇ KRONU</Isim>
			<CurrencyName>NORWEGIAN KRONE</CurrencyName>
			<ForexBuying>0.38312</ForexBuying>
			<ForexSelling>0.38569</ForexSelling>
			<BanknoteBuying>0.38285</BanknoteBuying>
			<BanknoteSelling>0.38658</BanknoteSelling>
				<CrossRateUSD>8.2103</CrossRateUSD>
				<CrossRateOther/>
		
	</Currency>
	<Currency CrossOrder="8" Kod="SAR" CurrencyCode="SAR">
			<Unit>1</Unit>
			<Isim>SUUDİ ARABİSTAN RİYALİ</Isim>
			<CurrencyName>SAUDI RIYAL</CurrencyName>
			<ForexBuying>0.84075</ForexBuying>
			<ForexSelling>0.84226</ForexSelling>
			<BanknoteBuying>0.83444</BanknoteBuying>
			<BanknoteSelling>0.84858</BanknoteSelling>
				<CrossRateUSD>3.7505</CrossRateUSD>
				<CrossRateOther/>
		
	</Currency>
	<Currency CrossOrder="5" Kod="JPY" CurrencyCode="JPY">
			<Unit>100</Unit>
			<Isim>JAPON YENİ</Isim>
			<CurrencyName>JAPENESE YEN</CurrencyName>
			<ForexBuying>3.0111</ForexBuying>
			<ForexSelling>3.0311</ForexSelling>
			<BanknoteBuying>3.0000</BanknoteBuying>
			<BanknoteSelling>3.0426</BanknoteSelling>
				<CrossRateUSD>104.47</CrossRateUSD>
				<CrossRateOther/>
		
	</Currency>
	<Currency CrossOrder="12" Kod="BGN" CurrencyCode="BGN">
			<Unit>1</Unit>
			<Isim>BULGAR LEVASI</Isim>
			<CurrencyName>BULGARIAN LEV</CurrencyName>
			<ForexBuying>1.7733</ForexBuying>
			<ForexSelling>1.7965</ForexSelling>
			<BanknoteBuying></BanknoteBuying>
			<BanknoteSelling></BanknoteSelling>
				<CrossRateUSD>1.7682</CrossRateUSD>
				<CrossRateOther/>
		
	</Currency>
	<Currency CrossOrder="13" Kod="RON" CurrencyCode="RON">
			<Unit>1</Unit>
			<Isim>RUMEN LEYİ</Isim>
			<CurrencyName>NEW LEU</CurrencyName>
			<ForexBuying>0.77098</ForexBuying>
			<ForexSelling>0.78106</ForexSelling>
			<BanknoteBuying></BanknoteBuying>
			<BanknoteSelling></BanknoteSelling>
				<CrossRateUSD>4.0670</CrossRateUSD>
				<CrossRateOther/>
		
	</Currency>
	<Currency CrossOrder="14" Kod="RUB" CurrencyCode="RUB">
			<Unit>1</Unit>
			<Isim>RUS RUBLESİ</Isim>
			<CurrencyName>RUSSIAN ROUBLE</CurrencyName>
			<ForexBuying>0.04905</ForexBuying>
			<ForexSelling>0.04970</ForexSelling>
			<BanknoteBuying></BanknoteBuying>
			<BanknoteSelling></BanknoteSelling>
				<CrossRateUSD>63.92</CrossRateUSD>
				<CrossRateOther/>
		
	</Currency>
	<Currency CrossOrder="15" Kod="IRR" CurrencyCode="IRR">
			<Unit>100</Unit>
			<Isim>İRAN RİYALİ</Isim>
			<CurrencyName>IRANIAN RIAL</CurrencyName>
			<ForexBuying>0.00985</ForexBuying>
			<ForexSelling>0.00998</ForexSelling>
			<BanknoteBuying></BanknoteBuying>
			<BanknoteSelling></BanknoteSelling>
				<CrossRateUSD>31834</CrossRateUSD>
				<CrossRateOther/>
		
	</Currency>
	<Currency CrossOrder="16" Kod="CNY" CurrencyCode="CNY">
			<Unit>1</Unit>
			<Isim>ÇİN YUANI</Isim>
			<CurrencyName>CHINESE RENMINBI</CurrencyName>
			<ForexBuying>0.46284</ForexBuying>
			<ForexSelling>0.46890</ForexSelling>
			<BanknoteBuying></BanknoteBuying>
			<BanknoteSelling></BanknoteSelling>
				<CrossRateUSD>6.7746</CrossRateUSD>
				<CrossRateOther/>
		
	</Currency>
	<Currency CrossOrder="17" Kod="PKR" CurrencyCode="PKR">
			<Unit>1</Unit>
			<Isim>PAKİSTAN RUPİSİ</Isim>
			<CurrencyName>PAKISTANI RUPEE</CurrencyName>
			<ForexBuying>0.02994</ForexBuying>
			<ForexSelling>0.03033</ForexSelling>
			<BanknoteBuying></BanknoteBuying>
			<BanknoteSelling></BanknoteSelling>
				<CrossRateUSD>104.72</CrossRateUSD>
				<CrossRateOther/>
		
	</Currency>
	<Currency CrossOrder="0" Kod="XDR" CurrencyCode="XDR">
		<Unit>1</Unit>
		<Isim>ÖZEL ÇEKME HAKKI (SDR)</Isim>
		<CurrencyName>SPECIAL DRAWING RIGHT (SDR)</CurrencyName>
		<ForexBuying>4.3586</ForexBuying>
		<ForexSelling/>
		<BanknoteBuying/>
		<BanknoteSelling/>
		<CrossRateUSD/>
		<CrossRateOther>1.38101</CrossRateOther>
	</Currency>
</Tarih_Date>

I did not create setup or executable for this code file. When I press F5 in Visual Studio Code or Alt+Shift+F10 in PyCharm you will see the following result.

/BlogEntryImages/146/TCMB_Window.png

 

You can see the Python code below.

 

#!/usr/bin/python

import requests
import xml.etree.ElementTree as ET
import tkinter as tk

class TCMBWindow(tk.Frame):

    def __init__(self, master, *args, **kwargs):

        tk.Frame.__init__(self, master, *args, **kwargs)

        table = tk.Frame(self)
        table.pack(side="top", fill="both", expand=True, ipadx=2, ipady=2)
        table.grid_rowconfigure(0, weight=1)
        table.grid_columnconfigure(0, weight=1)
        master.resizable(width=False, height=False)        

        element_tree = ET.ElementTree(ET.fromstring(requests.get("http://tcmb.gov.tr/kurlar/today.xml").text)).getroot()

        self.widgets = {}
        row = 1
                
        tk.Label(table, text="Date: "+element_tree.get("Date")).grid(row=0, column=0, sticky="nsew", padx=5, pady=2)
        tk.Label(table, text="Tarih: "+element_tree.get("Tarih")).grid(row=0, column=1, sticky="nsew", padx=5, pady=2)
        tk.Label(table, text="Bulten No: "+element_tree.get("Bulten_No")).grid(row=0, column=2, sticky="nsew", padx=5, pady=2)
                
        tk.Label(table, text="Currency Code", relief=tk.RIDGE, font=("Helvetica", 10, "bold"), bg="orange").grid(row=1, column=0, sticky="nsew")
        tk.Label(table, text="Unit", relief=tk.RIDGE, font=("Helvetica", 10, "bold"), bg="orange").grid(row=1, column=1, sticky="nsew")
        tk.Label(table, text="Isim", relief=tk.RIDGE, font=("Helvetica", 10, "bold"), bg="orange").grid(row=1, column=2, sticky="nsew")
        tk.Label(table, text="Currency Name", relief=tk.RIDGE, font=("Helvetica", 10, "bold"), bg="orange").grid(row=1, column=3, sticky="nsew")
        tk.Label(table, text="Forex Buying", relief=tk.RIDGE, font=("Helvetica", 10, "bold"), bg="orange").grid(row=1, column=4, sticky="nsew")
        tk.Label(table, text="Forex Selling", relief=tk.RIDGE, font=("Helvetica", 10, "bold"), bg="orange").grid(row=1, column=5, sticky="nsew")
        tk.Label(table, text="Banknote Buying", relief=tk.RIDGE, font=("Helvetica", 10, "bold"), bg="orange").grid(row=1, column=6, sticky="nsew")
        tk.Label(table, text="Banknote Selling", relief=tk.RIDGE, font=("Helvetica", 10, "bold"), bg="orange").grid(row=1, column=7, sticky="nsew")
        
        for currency in element_tree.findall('Currency'):

            row += 1

            currencycode = currency.get('CurrencyCode')
            unit = currency.find('Unit').text
            isim = currency.find('Isim').text
            currencyname = currency.find('CurrencyName').text
            forexbuying = currency.find('ForexBuying').text
            if forexbuying is None: forexbuying = "-"
            forexselling = currency.find('ForexSelling').text
            if forexselling is None: forexselling = "-"
            banknotebuying = currency.find('BanknoteBuying').text
            if banknotebuying is None: banknotebuying = "-"
            banknoteselling = currency.find('BanknoteSelling').text
            if banknoteselling is None: banknoteselling = "-"

            self.widgets[currencycode] = {
                "currencycode": tk.Label(table, text=str(currencycode).strip(), relief=tk.RIDGE),
                "unit": tk.Label(table, text=str(unit).strip(), relief=tk.RIDGE),
                "isim": tk.Label(table, text=str(isim).strip(), relief=tk.RIDGE),        
                "currencyname": tk.Label(table, text=str(currencyname).strip(), relief=tk.RIDGE),
                "forexbuying": tk.Label(table, text=str(forexbuying).strip(), relief=tk.RIDGE),
                "forexselling": tk.Label(table, text=str(forexselling).strip(), relief=tk.RIDGE),
                "banknotebuying": tk.Label(table, text=str(banknotebuying).strip(), relief=tk.RIDGE),
                "banknoteselling": tk.Label(table, text=str(banknoteselling).strip(), relief=tk.RIDGE)
            }

            self.widgets[currencycode]["currencycode"].grid(row=row, column=0, sticky="nsew")
            self.widgets[currencycode]["unit"].grid(row=row, column=1, sticky="nsew")
            self.widgets[currencycode]["isim"].grid(row=row, column=2, sticky="nsew")
            self.widgets[currencycode]["currencyname"].grid(row=row, column=3, sticky="nsew")
            self.widgets[currencycode]["forexbuying"].grid(row=row, column=4, sticky="nsew")
            self.widgets[currencycode]["forexselling"].grid(row=row, column=5, sticky="nsew")
            self.widgets[currencycode]["banknotebuying"].grid(row=row, column=6, sticky="nsew")
            self.widgets[currencycode]["banknoteselling"].grid(row=row, column=7, sticky="nsew")

if __name__ == "__main__":
    root = tk.Tk()
    root.title("TCMB Exchange Rates")
    TCMBWindow(root).pack(fill="both", expand=True)    
    root.mainloop()

 

I put the code on GitHub as well.

https://github.com/MuratYasar/TCMBExchangeRates

Have a great day.


(In order to use this feature, you have to register.)

Tag Related Blog Entries

Raspberry Pi 3 & Python & DHT22 Sensor

Sunday, January 22, 2017 0   742  

TCMB Mobile Applications

Friday, January 15, 2016 1   1523  

The first windows phone 8 app sumbitted to the Windows Phone Store

Saturday, August 03, 2013 3   2584  

Indicative Exchange Rates Announced by the Central Bank of Turkey For Daily Basis

Saturday, May 22, 2010 0   860   1