KnowHow

技術的なメモを中心にまとめます。
検索にて調べることができます。

json.dumpsとpprintについて

登録日 :2024/10/10 08:58
カテゴリ :Python基礎

配列を見やすく表示する

import pprint
import json

l = ['apple', 'orange', 'banana', 'peach', 'mango']
l.insert(0, l[:])

d = {'a': 'A', 'b': 'B', 'c': {'x': {'y': 'Y'}}}

print('*** pprint ***')
pp = pprint.PrettyPrinter(indent=4, width=40)
pp.pprint(d)

print('*** json.dumps ***')
print(json.dumps(d, indent=4))

実行結果

*** pprint ***
{   'a': 'A',
    'b': 'B',
    'c': {'x': {'y': 'Y'}}}
*** json.dumps ***
{
    "a": "A",
    "b": "B",
    "c": {
        "x": {
            "y": "Y"
        }
    }
}