import yaml def yaml_encode(data, indent=None): return yaml.dump(data, indent=indent, allow_unicode=True, 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_decode(data_decode))