假設Python 2,我寫了一個函數pprint_multiline
,您可以自定義它。
可以自定義如下:
- 線路長度
- 項之間的分離器
- 線之間的分離器(用於行的兩個塊,
None
之間空行爲沒有行分隔空字符串全部)
- 各個項目的對齊,以及
- 的對齊字符。
該函數接受水平打印的列表列表,因此您需要在調用此函數之前先解壓字典,如以下示例所示。
這適用於字典中的任何值,假設它實現了__str__
函數(大多數Python對象都這樣做)。
功能:
def pprint_multilines(linelen=80, separator=' ', line_separator=None,
align='left', align_char=' ', lists=[]):
maxlen = max(len(lst) for lst in lists)
printed_lines = ['']*len(lists)
empty = True
printed = False
for i in xrange(maxlen):
max_item_len = max(len(str(lst[i])) for lst in lists)
if max_item_len + len(printed_lines[0]) + 1 > linelen:
if printed and line_separator is not None:
if len(line_separator) > 0:
print line_separator*(linelen/len(line_separator))
else:
print
for printed_line in printed_lines:
print printed_line
printed = True
printed_lines = ['']*len(lists)
empty = True
for lst_idx, lst in enumerate(lists):
if not empty:
printed_lines[lst_idx] += separator
if align == 'left':
printed_lines[lst_idx] += str(lst[i]).ljust(max_item_len, align_char)
else: # align == 'right'
printed_lines[lst_idx] += str(lst[i]).rjust(max_item_len, align_char)
else:
empty = False
if not empty:
if printed and line_separator is not None:
if len(line_separator) > 0:
print line_separator*(linelen/len(line_separator))
else:
print
for printed_line in printed_lines:
print printed_line
要看到它在行動,你可以看到下面的例子:
dict1 = {'a': 'first', 'b': 'second', 'c': 3, 'd': ['the', 'fourth']}
dict2 = {'a': 1, 'b':2, 'c':3, 'd':4}
dict3 = {'a': '1', 'b':'two', 'c':'three', 'd': 'fourth'}
list_of_keys = ['a','b','c','d']
pprint_multilines(linelen=15, separator=' ', align='left', align_char=' ',
line_separator='-',
lists=[[dict1[key] for key in list_of_keys],
[dict2[key] for key in list_of_keys],
[dict3[key] for key in list_of_keys]])
print
pprint_multilines(linelen=15, separator=' ', align='left', align_char=' ',
lists=[[dict1[key] for key in list_of_keys],
[dict2[key] for key in list_of_keys],
[dict3[key] for key in list_of_keys]])
print
pprint_multilines(linelen=25, separator=' ', align='left', align_char=' ',
line_separator='',
lists=[[dict1[key] for key in list_of_keys],
[dict2[key] for key in list_of_keys],
[dict3[key] for key in list_of_keys]])
print
pprint_multilines(linelen=45, separator=' ', align='left', align_char='_',
lists=[[dict1[key] for key in list_of_keys],
[dict2[key] for key in list_of_keys],
[dict3[key] for key in list_of_keys]])
print
pprint_multilines(linelen=45, separator=' ', align='right', align_char=' ',
lists=[[dict1[key] for key in list_of_keys],
[dict2[key] for key in list_of_keys],
[dict3[key] for key in list_of_keys]])
將輸出(有一些評論):
first second
1 2
1 two
--------------- <-- This is the line separator
3 <-- This next item fits in 15 chars, but
3 if appended to the previous line
three this item won't fit, so break the third item to new lines
--------------- <-- Note that this spans the specified line length
['the', 'fourth']
4
fourth
first second
1 2
1 two
3 <-- No line separator!
3
three
['the', 'fourth']
4
fourth
first second 3 <-- linelen=25, the third item now fits!
1 2 3
1 two three
<-- A blank line separator
['the', 'fourth']
4
fourth
first second 3____ ['the', 'fourth'] <-- Showing alignment chars
1____ 2_____ 3____ 4________________ <-- linelen=45, everything fits!
1____ two___ three fourth___________
first second 3 ['the', 'fourth'] <-- These blocks are right-aligned
1 2 3 4
1 two three fourth
我我不確定你想要結果如何。你是否希望在行結束之前打印這些值?或者你想要每行有四個值? – justhalf 2014-10-30 06:15:59
如果字典包含的值比可以放在一行更多的值,那麼我猜如果我能在新行開始之前設置一個預定義的值(如50個字符),那麼對於各種類型的字典來說,這將很好。 – dimb 2014-10-30 06:23:59