PyYAML
the home of various YAML implementations for Python.
How to install
loads
python의 json.loads
와 비슷한 함수를 사용하고 싶다면 full_load
함수를 사용하면 된다.
Enhance list indentation
다음과 같이 list 가 상위 depth 의 indent 로 출력될 경우:
다음과 같이 Dumper 를 재정의:
class IndentDumper(yaml.Dumper):
def increase_indent(self, flow=False, indentless=False):
return super(IndentDumper, self).increase_indent(flow, False)
다음과 같이 list 에 indent 가 매겨져서 출력된다:
단락별 New Line
다음과 같이 단락 이 다닥다닥 붙어있다면:
Dumper 재정의
class NewLineDumper(Dumper):
def write_line_break(self, data: Optional[str] = None) -> None:
super().write_line_break(data)
if self.indent == ROOT_INDENT:
super().write_line_break(data)
다음과 같이 출력된다:
Troubleshooting
덤프(Dump)시 중첩 컬렉션 오작동
다음과 같이 출력된다.
기본적으로 PyYAML은 중첩 된 컬렉션이 있는지 여부에 따라 컬렉션의 스타일을 선택합니다. 컬렉션에 중첩 된 컬렉션이있는 경우 block-style이 지정됩니다. 그렇지 않으면 flow-style을 갖게됩니다.
컬렉션이 항상 블록 스타일로 직렬화되도록하려면 dump()
의 default_flow_style
매개 변수를 False로 설정합니다.