Kategori
DevNet Pemrograman

Parsing Dengan Python

XML

Berikut merupakan contoh dari XML, dari file myfile.xml

devasc@labvm:~/labs/devnet-src/parsing$ cat myfile.xml 
<?xml version="1.0" encoding="UTF-8"?>
<rpc message-id="1"
 xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
 <edit-config>
  <target>
   <candidate/>
  </target>
  <default-operation>merge</default-operation>
  <test-option>set</test-option>
  <config>
   <int8.1
    xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0"
    nc:operation="create"
    xmlns="http://netconfcentral.org/ns/test">9</int8.1>
  </config>
 </edit-config>
</rpc>
devasc@labvm:~/labs/devnet-src/parsing$ 

buat script parsexml.py

import xml.etree.ElementTree as ET
import re
xml = ET.parse("myfile.xml")
root = xml.getroot()
ns = re.match('{.*}', root.tag).group(0)
editconf = root.find("{}edit-config".format(ns))
defop = editconf.find("{}default-operation".format(ns))
testop = editconf.find("{}test-option".format(ns))
print("The default-operation contains: {}".format(defop.text))
print("The test-option contains: {}".format(testop.text))
devasc@labvm:~/labs/devnet-src/parsing$ 

Jalankan file parsexml.py

devasc@labvm:~/labs/devnet-src/parsing$ python3 parsexml.py 
The default-operation contains: merge
The test-option contains: set
devasc@labvm:~/labs/devnet-src/parsing$ 

JSON

Buat sebuah file parsejson.py

import json
import yaml
with open('myfile.json','r') as json_file:
 ourjson = json.load(json_file)
print(ourjson)

Sementara untuk file yang akan diparsing adalah dibuat dengan nama myfile.json

devasc@labvm:~/labs/devnet-src/parsing$ cat myfile.json 
{
 "access_token":"ZDI3MGEyYzQtNmFlNS00NDNhLWFlNzAtZGVjNjE0MGU1OGZmZWNmZDEwN2ItYTU3",
 "expires_in":1209600,
 "refresh_token":"MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTEyMzQ1Njc4",
 "refreshtokenexpires_in":7776000
}
devasc@labvm:~/labs/devnet-src/parsing$ 

Kemudian jalan parsejson.py

devasc@labvm:~/labs/devnet-src/parsing$ python3 parsejson.py 
{'access_token': 'ZDI3MGEyYzQtNmFlNS00NDNhLWFlNzAtZGVjNjE0MGU1OGZmZWNmZDEwN2ItYTU3', 'expires_in': 1209600, 'refresh_token': 'MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTEyMzQ1Njc4', 'refreshtokenexpires_in': 7776000}
devasc@labvm:~/labs/devnet-src/parsing$

Lalu edit kembali parsejson.py, dengan menambahkan statement nilai token dan berapa banyak waktu yang dibutuhkan sampai dengan token expire

devasc@labvm:~/labs/devnet-src/parsing$ python3 parsejson.py 
{'access_token': 'ZDI3MGEyYzQtNmFlNS00NDNhLWFlNzAtZGVjNjE0MGU1OGZmZWNmZDEwN2ItYTU3', 'expires_in': 1209600, 'refresh_token': 'MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTEyMzQ1Njc4', 'refreshtokenexpires_in': 7776000}
The access token is: ZDI3MGEyYzQtNmFlNS00NDNhLWFlNzAtZGVjNjE0MGU1OGZmZWNmZDEwN2ItYTU3
The token expires in 1209600 seconds.
devasc@labvm:~/labs/devnet-src/parsing$ 

jika diamati telah terjadi perubahan parsing pada file sebelumnya.

Selanjutnya memparsing data JSON dalam format YAML, dengan menambahkan statement pada parsejson.py

import json
import yaml
with open('myfile.json','r') as json_file:
 ourjson = json.load(json_file)
print(ourjson)
print("The access token is: {}".format(ourjson['access_token']))
print("The token expires in {} seconds.".format(ourjson['expires_in']))
print("\n\n---")
print(yaml.dump(ourjson))

Kemudian jalankan lagi perintah parsejson.py

devasc@labvm:~/labs/devnet-src/parsing$ python3 parsejson.py 
{'access_token': 'ZDI3MGEyYzQtNmFlNS00NDNhLWFlNzAtZGVjNjE0MGU1OGZmZWNmZDEwN2ItYTU3', 'expires_in': 1209600, 'refresh_token': 'MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTEyMzQ1Njc4', 'refreshtokenexpires_in': 7776000}
The access token is: ZDI3MGEyYzQtNmFlNS00NDNhLWFlNzAtZGVjNjE0MGU1OGZmZWNmZDEwN2ItYTU3
The token expires in 1209600 seconds.


---
access_token: ZDI3MGEyYzQtNmFlNS00NDNhLWFlNzAtZGVjNjE0MGU1OGZmZWNmZDEwN2ItYTU3
expires_in: 1209600
refresh_token: MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTEyMzQ1Njc4
refreshtokenexpires_in: 7776000

devasc@labvm:~/labs/devnet-src/parsing$ 

YAML

Buat sebuah file myfile.yaml

devasc@labvm:~/labs/devnet-src/parsing$ cat myfile.yaml 
---
access_token: ZDI3MGEyYzQtNmFlNS00NDNhLWFlNzAtZGVjNjE0MGU1OGZmZWNmZDEwN2ItYTU3
expires_in: 1209600
refresh_token: MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTEyMzQ1Njc4
refreshtokenexpires_in: 7776000
devasc@labvm:~/labs/devnet-src/parsing$ 

Buat sebuah file parseyaml.py

devasc@labvm:~/labs/devnet-src/parsing$ cat parseyaml.py 
# Fill in this file with the code from parsing YAML exercise
import json
import yaml
with open('myfile.yaml','r') as yaml_file:
 ouryaml = yaml.safe_load(yaml_file)
print(ouryaml)

devasc@labvm:~/labs/devnet-src/parsing$ 

Jalankan parseyaml.py

devasc@labvm:~/labs/devnet-src/parsing$ python3 parseyaml.py 
{'access_token': 'ZDI3MGEyYzQtNmFlNS00NDNhLWFlNzAtZGVjNjE0MGU1OGZmZWNmZDEwN2ItYTU3', 'expires_in': 1209600, 'refresh_token': 'MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTEyMzQ1Njc4', 'refreshtokenexpires_in': 7776000}
devasc@labvm:~/labs/devnet-src/parsing$ 

Edit kembali parseyaml.py dengan menambahkan statement

devasc@labvm:~/labs/devnet-src/parsing$ cat parseyaml.py 
# Fill in this file with the code from parsing YAML exercise
import json
import yaml
with open('myfile.yaml','r') as yaml_file:
 ouryaml = yaml.safe_load(yaml_file)
print(ouryaml)
print("The access token is {}".format(ouryaml['access_token']))
print("The token expires in {} seconds.".format(ouryaml['expires_in']))
devasc@labvm:~/labs/devnet-src/parsing$ 

Jalan perintah untuk parseyaml.py

devasc@labvm:~/labs/devnet-src/parsing$ python3 parseyaml.py 
{'access_token': 'ZDI3MGEyYzQtNmFlNS00NDNhLWFlNzAtZGVjNjE0MGU1OGZmZWNmZDEwN2ItYTU3', 'expires_in': 1209600, 'refresh_token': 'MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTEyMzQ1Njc4', 'refreshtokenexpires_in': 7776000}
The access token is ZDI3MGEyYzQtNmFlNS00NDNhLWFlNzAtZGVjNjE0MGU1OGZmZWNmZDEwN2ItYTU3
The token expires in 1209600 seconds.
devasc@labvm:~/labs/devnet-src/parsing$

Parse data YAML dengan format JSON

devasc@labvm:~/labs/devnet-src/parsing$ cat parseyaml.py 
# Fill in this file with the code from parsing YAML exercise
import json
import yaml
with open('myfile.yaml','r') as yaml_file:
 ouryaml = yaml.safe_load(yaml_file)
print(ouryaml)
print("The access token is {}".format(ouryaml['access_token']))
print("The token expires in {} seconds.".format(ouryaml['expires_in']))
print("\n\n")
print(json.dumps(ouryaml, indent=4))
devasc@labvm:~/labs/devnet-src/parsing$ 

jalan perintah untuk parseyaml.py

devasc@labvm:~/labs/devnet-src/parsing$ python3 parseyaml.py 
{'access_token': 'ZDI3MGEyYzQtNmFlNS00NDNhLWFlNzAtZGVjNjE0MGU1OGZmZWNmZDEwN2ItYTU3', 'expires_in': 1209600, 'refresh_token': 'MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTEyMzQ1Njc4', 'refreshtokenexpires_in': 7776000}
The access token is ZDI3MGEyYzQtNmFlNS00NDNhLWFlNzAtZGVjNjE0MGU1OGZmZWNmZDEwN2ItYTU3
The token expires in 1209600 seconds.



{
    "access_token": "ZDI3MGEyYzQtNmFlNS00NDNhLWFlNzAtZGVjNjE0MGU1OGZmZWNmZDEwN2ItYTU3",
    "expires_in": 1209600,
    "refresh_token": "MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDEyMzQ1Njc4OTEyMzQ1Njc4",
    "refreshtokenexpires_in": 7776000
}
devasc@labvm:~/labs/devnet-src/parsing$ 

Ref : [1][2]