4章03
yaml形式を読み込む
元のyamlファイルの中身
title: Fruits Database
version: 3.2
price-define:
- &one-coin 100
- &two-coin 200
- &three-coin 300
items:
- name: Tomato
price: three-coin
- name: Banana
price: two-coin
- name: Apple
price: two-coin
- name: Kiwi
price: three-coin
終わり
import yaml
## YAMLドキュメントを読み込む
string = open('test.yml').read()
data = yaml.load(string)
for item in data['items']:
print(item['name'],item['price'])
<結果>
Tomato 300
Banana 200
Apple 200
Kiwi 300
ini形式の設定ファイルを読み込む
もとのファイルの中身
; Fruits Database
; version 1.2
[Banana]
price=300
color=yellow
[Apple]
price=300
color=red
[Mango]
price=320
color=yellow
[Strawberry]
price=430
color=red
import configparser
config = configparser.ConfigParser()
config.read('test.ini')
for item in config.sections():
print(item,config[item]['Price'])
<結果>
Banana 300
Apple 300
Mango 320
Strawberry 430