개발일기 [Python 파이썬]

[Python pprint]코드 예쁘게 출력하기

neullo 2024. 2. 21. 13:24

pprint

from pprint import pprint

터미널에 한 줄로 복잡하게 출력되는 것을 한눈에 볼 수 있게 만들어 주는 코드로 pprint라는 모듈을 먼저 import 해주어야 사용 가능합니다.  변수에 많은 양의 데어터가 담겨 있는데, 이 안에 있는 내용을 좀 더 깔끔하게, 인덴트도 다 맞춰서, 이쁘게  출력해줍니다.

 

pprint 사용 전 결과값을 보여주는 터미널 모습

 

pprint 사용 후 결과값을 보여주는 터미널 모습

 

 

# 사용할 코드

# pprint는 pretty print의 약자이며, 데이터를 더 예쁘게 출력해 줍니다.
from pprint import pprint

sample_data = {
    "id": "0001",
    "type": "donut",
    "name": "Cake",
    "ppu": 0.55,
    "batters":
        {
            "batter":
                [
                    {"id": "1001", "type": "Regular"},
                    {"id": "1002", "type": "Chocolate"},
                    {"id": "1003", "type": "Blueberry"},
                    {"id": "1004", "type": "Devil's Food"}
                ]
        },
    "topping":
        [
            {"id": "5001", "type": "None"},
            {"id": "5002", "type": "Glazed"},
            {"id": "5005", "type": "Sugar"},
            {"id": "5007", "type": "Powdered Sugar"},
            {"id": "5006", "type": "Chocolate with Sprinkles"},
            {"id": "5003", "type": "Chocolate"},
            {"id": "5004", "type": "Maple"}
        ]
}

pprint(sample_data) # print로 출력했을 때와 결과 비교해 보기!!