|
% pip3 install pyyaml
% cat yaml_test.py
import yaml
data = {'A': 'abc', 'B': {'C': 'c', 'D': 'd', 'E': 'e'}, 'W': ['x', 'y', 'z']}
with open('data.yml', 'w') as file:
yaml.dump(data, file, default_flow_style=False)
file.write("---\n")
yaml.dump(data, file, default_flow_style=False)
#with open('data.yml') as f:
# film = yaml.load(f, Loader=yaml.FullLoader)
# print("KK1",film)
with open('data.yml') as f:
films = yaml.load_all(f, Loader=yaml.FullLoader)
for film in films:
% cat test2.py
import yaml
yaml_str = """
Date: 2017-03-10
PriceList:
-
item_id: 1000
name: Banana
color: yellow
price: 800
-
item_id: 1001
name: Orange
color: orange
price: 1400
-
item_id: 1002
name: Apple
color: red
price: 2400
"""
#-´Â ¹è¿À» ¶æÇÔ
data = yaml.load(yaml_str)
for item in data['PriceList']:
print("id: ",item['item_id'],"name: ",item['name'],"color: ",item['color'],"price: ",item['price'])
print("KK2",film) |
|