Created
November 9, 2019 08:16
-
-
Save Wegazz/e5826c8ba6e6bc068e8886cee95668b0 to your computer and use it in GitHub Desktop.
Работа с YAML файлами настрек
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import yaml | |
import io | |
configFile = 'test_yaml.yaml' | |
# 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(configFile, 'w', encoding='utf8') as outfile: | |
yaml.dump(data, outfile, default_flow_style=False, allow_unicode=True) | |
# Read YAML file | |
with open(configFile, 'r', encoding='utf8') as stream: | |
data_loaded = yaml.safe_load(stream) | |
print("Данные идентичны" if data == data_loaded else "Данные разнятся") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment