xxxxxxxxxx
import yaml
# Path to the YAML file
file_path = "path/to/your/file.yaml"
# Open the file and load the YAML data
with open(file_path, 'r') as file:
yaml_data = yaml.load(file, Loader=yaml.FullLoader)
# Process the YAML data
# ...
# Access specific values in the YAML data
# Example: Assuming the YAML structure has a 'key' field
value = yaml_data['key']
# Print the value
print(value)
xxxxxxxxxx
# read_categories.py file
import yaml
with open(r'E:\data\categories.yaml') as file:
documents = yaml.full_load(file)
for item, doc in documents.items():
print(item, ":", doc)
xxxxxxxxxx
import yaml # pip install pyyaml
cfg_values = yaml.load(open(cfg_file), Loader=yaml.FullLoader)
xxxxxxxxxx
# -*- coding: utf-8 -*-
import yaml
import io
# Define data
data = {
'a list': [
1,
42,
3.141,
1337,
'help',
u'€'
],
'a string': 'bla',
'another dict': {
'foo': 'bar',
'key': 'value',
'the answer': 42
}
}
# Write YAML file
with io.open('data.yaml', 'w', encoding='utf8') as outfile:
yaml.dump(data, outfile, default_flow_style=False, allow_unicode=True)
# Read YAML file
with open("data.yaml", 'r') as stream:
data_loaded = yaml.safe_load(stream)
print(data == data_loaded)
xxxxxxxxxx
{'a_key': 'a_value', 'another_key': 'another_value', 'nested_dictionary': {'nested_key': 'nested_value'}}