본문 바로가기
카테고리 없음

파이썬 강의 - prettyprint를 가장 잘 설명하는 방법

by 뷰티풀스택 2023. 7. 8.
반응형

제목: prettyprint

 

prettyprint는 데이터를 보기 좋게 출력하는 방법을 말합니다. 이는 주로 디버깅이나 데이터 시각화를 위해 사용됩니다.

 

파이썬에는 prettyprint를 위한 내장 모듈인 'pprint'가 있습니다. pprint 모듈은 재귀적으로(iterative) 복잡한 데이터 구조를 탐색하고, 들여쓰기와 줄 바꿈을 적절하게 적용하여 가독성을 높여줍니다.

 

아래는 pprint 모듈을 사용하여 데이터를 예쁘게 출력하는 간단한 예제입니다:

 

```python

import pprint

 

data = {

    'name': 'John Doe',

    'age': 30,

    'address': {

        'street': '123 Example Street',

        'city': 'Example City',

        'state': 'Example State'

    },

    'friends': ['Jane Smith', 'Bob Johnson']

}

 

pprint.pprint(data)

```

 

위의 예제에서 `pprint.pprint()` 함수를 사용하여 data 딕셔너리를 예쁘게 출력합니다.

 

출력 결과 예시:

```python

{

    'address': {

        'city': 'Example City',

        'state': 'Example State',

        'street': '123 Example Street'

    },

    'age': 30,

    'friends': ['Jane Smith', 'Bob Johnson'],

    'name': 'John Doe'

}

```

 

pprint 모듈을 사용하면 자료 구조의 구조와 계층적인 관계를 가시적으로 파악할 수 있습니다. 이는 디버깅 시에 유용하며, 출력 결과를 읽기 쉽게 만드는 데 도움이 됩니다.

 

자세한 내용은 파이썬 공식 문서의 pprint 모듈 문서를 참고하시기 바랍니다.

 

[Python pprint 모듈 문서](https://docs.python.org/3/library/pprint.html)

반응형

댓글