파이썬[Python]: pprint - PrettyPrinter 클래스

pprint 모듈 - PrettyPrinter 클래스(class)


/// 설명

PrettyPrinter 인스턴스를 생성합니다.

indent 는 들여쓰기를 나타냅니다.
width 는출력할 수 있는 최대 넓이를 나타냅니다.
depth 는 표현할 수 있는 데이터 구조의 깊이를 나타냅니다. 데이터 구조가 깊다면 '...' 으로 표현을 합니다.
stream 은 출력하는 파일 객체를 나타내며, 기본값으로 sys.stdout 이 설정되어 있습니다.
compact 는 최대 넓이 안에서 라인 단위로 표현할지 결정합니다. False라면 라인단위로 출력합니다.
sort_dicts 는 정렬을 결정합니다. True라면 정렬 후 출력합니다.
underscore_numbers 는 정수를 표현함에 있어서 접두사로 '_'를 사용합니다. True일 경우 사용합니다.

※ 형식
class pprint.PrettyPrinter(indent=1, width=80, depth=None, stream=None, *, compact=False, sort_dicts=True, underscore_numbers=False)

reference
https://docs.python.org/3/library/pprint.html#module-pprint

/// 예제

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import sys
import pprint
 
test = ['Other''values''configure''the''manner''in''which',
        'nesting''of']
 
test_pp = pprint.PrettyPrinter(indent=4, stream=sys.stdout)
test_pp.pprint(test)
# [   'Other',
#     'values',
#     'configure',
#     'the',
#     'manner',
#     'in',
#     'which',
#     'nesting',
#     'of']
 
test_pp = pprint.PrettyPrinter(indent=8, stream=sys.stdout)
test_pp.pprint(test)
# [       'Other',
#         'values',
#         'configure',
#         'the',
#         'manner',
#         'in',
#         'which',
#         'nesting',
#         'of']
 
test = ['Other''values''configure''the''manner''in''which',
        'nesting''of''complex''data''structures''is''displayed']
 
test_pp = pprint.PrettyPrinter(indent=4, stream=sys.stdout, compact=True)
test_pp.pprint(test)
# [   'Other', 'values', 'configure', 'the', 'manner', 'in', 'which', 'nesting',
#     'of', 'complex', 'data', 'structures', 'is', 'displayed']
 
test_pp = pprint.PrettyPrinter(indent=4, width=40, stream=sys.stdout, compact=True)
test_pp.pprint(test)
# [   'Other', 'values', 'configure',
#     'the', 'manner', 'in', 'which',
#     'nesting', 'of', 'complex', 'data',
#     'structures', 'is', 'displayed']
 
test = ['Other''values', ['configure', ['the''manner', ['in''which']]],
        'nesting''of', ['complex''data''structures'], 'is''displayed']
 
test_pp = pprint.PrettyPrinter(indent=4, depth=3)
test_pp.pprint(test)
# [   'Other',
#     'values',
#     ['configure', ['the', 'manner', [...]]],
#     'nesting',
#     'of',
#     ['complex', 'data', 'structures'],
#     'is',
#     'displayed']
cs

* 실행환경: Microsoft Windows 10 Homes
* 인터프리터: 파이썬(Python 3.9)


– 당신을 응원합니다. –

댓글

이 블로그의 인기 게시물

파이썬[Python]: 내장함수 - from_bytes 메서드

파이썬[Python]: 내장함수 - __len__ 메서드

파이썬[Python]: kivy - 한글 사용

파이썬[Python]: 내장함수 - bit_length 메서드

C 언어: sin 함수, cos 함수, tan 함수