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.
https://www.edureka.co/community/51409/what-is-the-difference-between-print-and-pprint-in-python
반응형
'python-기타' 카테고리의 다른 글
Google Analytics Data API과 Python을 활용한 데이터 수집(Data collection using Google Analytics Data API and Python) (0) | 2023.05.11 |
---|---|
split(), split(" ")의 차이점 (0) | 2022.10.26 |
Python isnumeric()에 관한 건 (0) | 2022.10.21 |
reverse 와 reversed 차이 (0) | 2021.12.28 |
댓글