본문 바로가기
python-기타

print vs pprint

by 무적김두칠 2021. 12. 30.
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
from pprint import pprint
sample_json = {
    "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" }
        ]
}
 
print(sample_json)
pprint(sample_json)
cs

위와 같은 Json형식이 있으면

1
2
3
{'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''Chocol
ate'}, {'id''5004''type''Maple'}]}
cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{'batters': {'batter': [{'id''1001''type''Regular'},
                        {'id''1002''type''Chocolate'},
                        {'id''1003''type''Blueberry'},
                        {'id''1004''type'"Devil's Food"}]},
 'id''0001',
 'name''Cake',
 'ppu'0.55,
 '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'}],
 'type''donut'}
cs

위가 print 아래가 pprint
보기 이쁘게(?) 출력하는 차이가 있음.

실제로 pprint means "pretty print"라고 하는 설도 있네요

출처:https://amalgjose.com/2021/05/09/what-is-the-difference-between-print-and-pprint-in-python/#:~:text=The%20purpose%20is%20very%20simple,content%20in%20a%20single%20line.

 

What is the difference between print() and pprint() in Python ?

Anyone who knows the basics of python are familiar with print() function. The purpose is very simple, it is for printing anything in python. pprint() function also has similar functionality. But th…

amalgjose.com

https://www.edureka.co/community/51409/what-is-the-difference-between-print-and-pprint-in-python

 

What is the difference between print and pprint in Python

I am beginner in Python. I want to know the difference between print and pprint in Python. Any help would be appreciated

www.edureka.co

 

반응형

댓글