28 lines
630 B
Python
28 lines
630 B
Python
import yaml
|
|
|
|
|
|
def yaml_encode(data, indent=None, unicode=False):
|
|
return yaml.dump(data, indent=indent, allow_unicode=not unicode, sort_keys=False)
|
|
|
|
|
|
def yaml_decode(data):
|
|
return yaml.load(data, Loader=yaml.FullLoader)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
# Example.
|
|
data_encode = {"id": 101, "name": "小明", "friends": ["小红", "小花"], "list": {"a": 1, "b": 5}}
|
|
data_decode = """
|
|
id: 101
|
|
name: 小明
|
|
friends:
|
|
- 小红
|
|
- 小花
|
|
list:
|
|
a: 1
|
|
b: 5
|
|
"""
|
|
print(yaml_encode(data_encode))
|
|
print(yaml_encode(data_encode, unicode=True))
|
|
print(yaml_decode(data_decode))
|