LoginSignup
21
18

More than 5 years have passed since last update.

No.042【Python】pprintの使い方

Posted at

python-logo-master-v3-TM-flattened.png

今回は、pprintの使いかたについて書いていきます。

I'll write about "how to use "pprint"

■ pprintモジュール

 リスト(list型)や辞書(dict型)などのオブジェクトを綺麗に出力・表示、
 また、文字列(str型オブジェクト)に変換することが可能。
 You can output and indicate objects such as lists and dictionaries with 
 better loonking.
>>> # pprintの使い方
>>> 
>>> 
>>> import pprint
>>> l = [{"Name": "Youtarin XXX", "Age":18, "Points":[70, 30]},{"Name": "No-tarin YYY", "Age":19, "Points":[40, 60]},{"Name": "tarin ZZZ", "Age":20, "Points":[50, 50]}]
>>> 
>>> print(l)
[{'Name': 'Youtarin XXX', 'Age': 18, 'Points': [70, 30]}, {'Name': 'No-tarin YYY', 'Age': 19, 'Points': [40, 60]}, {'Name': 'tarin ZZZ', 'Age': 20, 'Points': [50, 50]}]

■ 出力幅を指定:width

 Indicate the number of strings:width

>>> # 文字数を引数widthで指定することが可能
>>> 
>>> pprint.pprint(l, width=40)
[{'Age': 18,
  'Name': 'Youtarin XXX',
  'Points': [70, 30]},
 {'Age': 19,
  'Name': 'No-tarin YYY',
  'Points': [40, 60]},
 {'Age': 20,
  'Name': 'tarin ZZZ',
  'Points': [50, 50]}]
>>> # 大きな値を指定すると、改行挿入されずprint()と同様の出力となる
>>> 
>>> pprint.pprint(l, width=150)
[{'Age': 18, 'Name': 'Youtarin XXX', 'Points': [70, 30]},
 {'Age': 19, 'Name': 'No-tarin YYY', 'Points': [40, 60]},
 {'Age': 20, 'Name': 'tarin ZZZ', 'Points': [50, 50]}]

>>> # 改行されるのはリストや辞書の要素ごと
>>> # keyとvalueで改行、数値の途中で改行されしない
>>> 
>>> pprint.pprint(l, width = 1)
[{'Age': 18,
  'Name': 'Youtarin '
          'XXX',
  'Points': [70,
             30]},
 {'Age': 19,
  'Name': 'No-tarin '
          'YYY',
  'Points': [40,
             60]},
 {'Age': 20,
  'Name': 'tarin '
          'ZZZ',
  'Points': [50,
             50]}]
>>> # ↑ 文字列は単語ごとに改行される

■ 出力要素の深さを指定:depth

 Indicate the depth of output factors

>>> # 指定した値より深い要素は省略記号にて出力される
>>> 
>>> pprint.pprint(l, depth = 1)
[{...}, {...}, {...}]
>>> 
>>> pprint.pprint(l, depth = 2)
[{'Age': 18, 'Name': 'Youtarin XXX', 'Points': [...]},
 {'Age': 19, 'Name': 'No-tarin YYY', 'Points': [...]},
 {'Age': 20, 'Name': 'tarin ZZZ', 'Points': [...]}]
>>> 
>>> # デフォルトはdepth=Noneで全ての要素が省略されず出力される
>>> # widthの組み合わせでも可能
>>> # 改行される場所は width で指定する文字数に依存する
>>> 
>>> pprint.pprint(l, depth = 2, width = 40)
[{'Age': 18,
  'Name': 'Youtarin XXX',
  'Points': [...]},
 {'Age': 19,
  'Name': 'No-tarin YYY',
  'Points': [...]},
 {'Age': 20,
  'Name': 'tarin ZZZ',
  'Points': [...]}]

■ インデント幅の指定:indent

 Indicate indent width

>>> # インデント幅を引数 indentで指定できる
>>> # デフォルトは indent = 1
>>> 
>>> pprint.pprint(l, indent = 4, width = 4)
[   {   'Age': 18,
        'Name': 'Youtarin '
                'XXX',
        'Points': [   70,
                      30]},
    {   'Age': 19,
        'Name': 'No-tarin '
                'YYY',
        'Points': [   40,
                      60]},
    {   'Age': 20,
        'Name': 'tarin '
                'ZZZ',
        'Points': [   50,
                      50]}]

■ 改行を最小限にする:compact

 Minimize new lines:compact

>>> # デフォルトで width に収まらない場合、全ての要素は改行される
>>> 
>>> l_long = [list(range(10)), list(range(100, 120))]
>>> 
>>> print(l_long)
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119]]
>>> # compactを"True"とすると"width"に収まらない分改行される
>>> # 要素数の多いリストがある場合、compact = Trueの方が見やすい
>>> 
>>> pprint.pprint(l_long, width = 40, compact = True)
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 [100, 101, 102, 103, 104, 105, 106,
  107, 108, 109, 110, 111, 112, 113,
  114, 115, 116, 117, 118, 119]]

■ 文字列に変換: pprint.pformat()

 Convert to strings

>>> # 辞書やリストはstr( )で文字列に変換できる
>>> 
>>> s_normal = str(l)
>>> 
>>> print(s_normal)
[{'Name': 'Youtarin XXX', 'Age': 18, 'Points': [70, 30]}, {'Name': 'No-tarin YYY', 'Age': 19, 'Points': [40, 60]}, {'Name': 'tarin ZZZ', 'Age': 20, 'Points': [50, 50]}]
>>> 
>>> print(type(s_normal))
<class 'str'>
>>> # pprint.pformat():適宜改行が挿入され、整形された文字列として取得可能
>>> 
>>> s_pp = pprint.pformat(l)
>>> 
>>> print(s_pp)
[{'Age': 18, 'Name': 'Youtarin XXX', 'Points': [70, 30]},
 {'Age': 19, 'Name': 'No-tarin YYY', 'Points': [40, 60]},
 {'Age': 20, 'Name': 'tarin ZZZ', 'Points': [50, 50]}]
>>> 
>>> print(type(s_pp))
<class 'str'>
>>> # pprint.pformat() = pprint.pprint()
>>> 
>>> s_pp = pprint.pformat(l, depth = 2, width = 40, indent = 2)
>>> 
>>> print(s_pp)
[ { 'Age': 18,
    'Name': 'Youtarin XXX',
    'Points': [...]},
  { 'Age': 19,
    'Name': 'No-tarin YYY',
    'Points': [...]},
  { 'Age': 20,
    'Name': 'tarin ZZZ',
    'Points': [...]}]

■ 二次元配列の整形

 Shaping a two-dimensional array

>>> # pprintは二次元配列表示の際は便利
>>> 
>>> l_2d = [list(range(10)), list(range(10)), list(range(10))]
>>> 
>>> print(l_2d)
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]
>>> 
>>> pprint.pprint(l_2d)
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]
>>> # 要素数が少ない場合、デフォルトのwidth = 80 に収まるため改行されない
>>> 
>>> l_2d = [[0, 1, 2], [3, 4, 5], [6, 7, 8]]
>>> 
>>> print(l_2d)
[[0, 1, 2], [3, 4, 5], [6, 7, 8]]
>>> 
>>> pprint.pprint(l_2d)
[[0, 1, 2], [3, 4, 5], [6, 7, 8]]
>>> # 改行表示したい場合 widthを適宜指定する
>>> 
>>> pprint.pprint(l_2d, width = 20)
[[0, 1, 2],
 [3, 4, 5],
 [6, 7, 8]]
>>> 
>>> # 文字列として取得したい場合:pprint.pformat( )
>>> 
>>> s = pprint.pformat(l_2d, width = 20)
>>> 
>>> print(s)
[[0, 1, 2],
 [3, 4, 5],
 [6, 7, 8]]
>>> 
>>> print(type(s))
<class 'str'>

随時に更新していきますので、
定期的な購読をよろしくお願いします。
I'll update my article at all times.
So, please subscribe my articles from now on.

本記事について、
何か要望等ありましたら、気軽にメッセージをください!
If you have some requests, please leave some messages! by You-Tarin

21
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
21
18