{ "node":
{ "hostname": "Edge1", "details": { "interface":
{ "name": "Ethernet0", "description": "L2 Uplink", "enabled": "true"
}
}
}
}
- Python file:
#!/usr/bin/env python
""" simple code to parse json """
import json
with open("data.json", "r", encoding = "UTF-8") as file:
data = json.load(file)
print(data)
- Output:
python parse_json.py
{'node': {'hostname': 'Edge1', 'details': {'interface': {'name': 'Ethernet0', 'description': 'L2 Uplink', 'enabled': ‘true'}}}}
*** of course we can narrow the output information by modifying print command to for instance:
print(data['node']['hostname'])
python parse_json.py
Edge1
cat data.yaml
---
device:
hostname: Edge1
interface:
name:Ethernet0
description:Uplink
enabled:true
...
- Python file:
cat parse_yaml.py
""" parse YAML file simple example """
import yaml
with open ("data.yaml", "r", encoding = "UTF-8") as fdata:
ydata = yaml.safe_load(fdata)
print(ydata)
- Output
python parse_yaml.py
{'device': {'hostname': 'Edge1', 'interface': 'name:Ethernet0 description:Uplink enabled:true’}}
No comments:
Post a Comment