yaml 파일 다루기는 json 파일 다루기와 비슷하다.
python에서 yaml 파일을 다루기 위해서는 아리 명령을 통해 pyyaml 을 설치해야한다.
pip install pyyaml
#sample.yaml
sample:
description: Demonstration of the CCS811 Digital Gas Sensor driver
name: CCS811 sample
tests:
sample.sensor.ccs811:
harness: sensor
tags: sensors
platform_allow: thingy52_nrf52832 efr32mg_sltb004a
integration_platforms:
- efr32mg_sltb004a
depends_on: i2c
import yaml
with open('sample.yaml') as f:
data = yaml.load(f, Loader=yaml.FullLoader)
print(data)
#print(data['sample']['name'])
print("-"*30)
# sorted by key
data_sorted = yaml.dump(data, sort_keys=True)
print(data_sorted)
predefined_data = {'sample': {'description': 'Demonstration of the CCS811 Digital Gas Sensor driver', 'name': 'CCS811 sample'}}
with open('sample_output.yaml', 'w') as fw:
yaml.dump(predefined_data, fw)
위 예제는 sample.yaml 파일을 읽어들여 출력하고, predefined_data 에 저장된 값을 sample_output.yaml 파일에 write하는 코드이다.
[파이썬(Python)] #19. subprocess 모듈 (0) | 2021.10.15 |
---|---|
[파이썬(Python)] #30 sys.argv 사용하기 (0) | 2021.10.14 |
[파이썬(Python)] #27 None Value (0) | 2021.09.28 |
[파이썬(Python)] #28 매직 메소드(magic methods) (0) | 2021.09.27 |
[파이썬(Python)] #26 문자열 템플릿 (string template) (2) | 2021.09.27 |