YAML is a configuration file format
The file itself might look like this:
You can read it like this:
That's all you need. Now the entire yaml file is in 'MyDict' dictionary.
Output:
The file itself might look like this:
mysql:
host: localhost
user: root
passwd: my secret password
db: write-math
other:
preprocessing_queue:
- preprocessing.scale_and_center
- preprocessing.dot_reduction
- preprocessing.connect_lines
use_anonymous: yes
You can read it like this:
#!/usr/bin/env python import yaml #Import yaml module with open("example.yml", 'r') as stream: #open the yaml file in read mode try: myDict = (yaml.load(stream)) #load the file into a dictionary called 'Dict' for section in myDict: print section print myDict['mysql'] except yaml.YAMLError as exc: print(exc)
That's all you need. Now the entire yaml file is in 'MyDict' dictionary.
Output:
other
mysql
{'passwd': 'my secret password',
'host': 'localhost',
'db': 'write-math',
'user': 'root'}
Comments
Post a Comment