단축어 의미
Yet Another Markup Language (YAML)
-> 또 다른 마크업 랭귀지의 의미
YAML Ain't Markup Language (YAML)
-> 하지만, 데이타 기반의 의미로..마크업 랭귀지가 아니다.
시작은 --- (3개의 대시로 시작)
한 파일에서 여러번 쓰일 수 있고, 각각의 YAML 문서의 시작으로 표현된다.
각각의 문서의 마지막은 ... 으로 마무리 한다.
YAML 기본 형식
---
doe: "a deer, a female deer"
ray: "a drop of golden sun"
pi: 3.14159
xmas: true
french-hens: 3
calling-birds:
- huey
- dewey
- louie
- fred
xmas-fifth-day:
calling-birds: four
french-hens: 3
golden-rings: 5
partridges:
count: 1
location: "a pear tree"
turtle-doves: two
동일한 내용의 JSON 파일
{
"doe": "a deer, a female deer",
"ray": "a drop of golden sun",
"pi": 3.14159,
"xmas": true,
"french-hens": 3,
"calling-birds": [
"huey",
"dewey",
"louie",
"fred"
],
"xmas-fifth-day": {
"calling-birds": "four",
"french-hens": 3,
"golden-rings": 5,
"partridges": {
"count": 1,
"location": "a pear tree"
},
"turtle-doves": "two"
}
}
YAML 파일이 읽기도 쓰기도 쉽다.
YAML 포맷 특징
스페이스 공백 2개 (indentation)
탭을 사용하지 않는다.
들려쓰기를 통한 중첩
YAML 데이타 타입
- Key-Value and Dictionary
- Numeric
- String
- Booleans
- Null
- Arrays
YAML 문자열 처리
문자열 기본 형식
---
foo: this is a normal string
foo: "this is a normal string"
foo: 'this is a normal string'
특수문자 처리
---
foo: "this is not a normal string\n"
bar: this is not a normal string\n
>> 출력
foo: this is not a normal string
bar: this is not a normal string\n
// 쌍따옴표에 의해서 특수문자가 처리된다.
여러줄의 문자열 처리 >
bar: >
this is not a normal string it
spans more than
one line
see?
하지만 출력시 한줄로 표시됨.
>> 출력
bar : this is not a normal string it spans more than one line see?
여러줄의 문자열 처리 |
bar: |
this is not a normal string it
spans more than
one line
see?
>> 출력
bar : this is not a normal string it
spans more than
one line
see?
Nulls 타입
---
foo: ~
bar: null
Booleans 타입
---
foo: True
bar: False
light: On
TV: Off
Arrays 타입
---
items: [ 1, 2, 3, 4, 5 ]
names: [ "one", "two", "three", "four" ]
---
items:
- 1
- 2
- 3
- 4
- 5
names:
- "one"
- "two"
- "three"
- "four"
___
items:
- things:
thing1: huey
things2: dewey
thing3: louie
- other things:
key: value
Dictionary (Key-Value) 타입
---
foo: { thing1: huey, thing2: louie, thing3: dewey }
주석처리
___
# This is a full line comment
foo: bar # this is a comment, too
파이썬에 YAML 파일 읽어 들이기
Import yaml
from yaml import load
try:
from yaml import CLoader as Loader
except ImportError:
from yaml import Loader
if __name__ == '__main__':
stream = open("foo.yaml", 'r')
dictionary = yaml.load(stream)
for key, value in dictionary.items():
print (key + " : " + str(value))
'프로그래밍' 카테고리의 다른 글
WSGI(Web Server Gateway Interface)란 ? (0) | 2025.04.07 |
---|---|
백엔드서버 구축방법 > LAMP: Linux + Apache + MySQL + PHP with Ubuntu (0) | 2025.04.07 |
백엔드 서버를 구축하는 방법 (0) | 2025.04.07 |
Github Clone Private - 비공개 저장소 복사하기 (0) | 2024.11.15 |
OneNote 에서 프로그래밍 소스코드 입력시 템플릿 사용 플러그인 (3) | 2021.10.24 |