本文整理汇总了Python中textwrap.TextWrapper.indent方法的典型用法代码示例。如果您正苦于以下问题:Python TextWrapper.indent方法的具体用法?Python TextWrapper.indent怎么用?Python TextWrapper.indent使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类textwrap.TextWrapper
的用法示例。
在下文中一共展示了TextWrapper.indent方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: pprint_table
# 需要导入模块: from textwrap import TextWrapper [as 别名]
# 或者: from textwrap.TextWrapper import indent [as 别名]
def pprint_table(out, table, justs=None, separator=None, outer_seps=False, \
widths=None, blank_row=False, default_just=None, hanging_indent=0):
"""
Prints out a table of data, padded for alignment.
Each row must have the same number of columns.
Cells may include line breaks.
@param out: output stream
@type out: file-like object
@param table: table to print.
@type table: list of lists
@param outer_seps: Prints separators at the start and end of each row
if true.
@type outer_seps: bool
@type widths: list of ints
@param widths: maximum width for each column. None means no maximum is
imposed. Words are wrapped if the width exceeds the maximum
@type default_just: bool
@param default_just: the default justification to use for all columns
if C{justs} is not given or where a column's justification is not
given. Default False
@type hanging_indent: int
@param hanging_indent: hanging indent to apply to the column if a cell
is wrapped (number of spaces)
"""
col_paddings = []
wrapper = TextWrapper()
if hanging_indent:
wrapper.indent = ''
wrapper.subsequent_indent = ' '*hanging_indent
# Format any numbers in the table
table = [
[format_num(cell) for cell in row]
for row in table]
# Work out the maximum width of each column so we know how much to pad
for i in range(len(table[0])):
if widths is not None and widths[i] is not None:
col_paddings.append(widths[i])
else:
col_paddings.append(get_max_width(table, i))
# Work out justification of each column
coljusts = []
if default_just is None:
default_just = False
for col in range(len(table[0])):
if justs:
if justs[col] is not None:
coljust = justs[col]
else:
coljust = default_just
else:
coljust = default_just
coljusts.append(coljust)
# Wrap the long cells that have a max width
multiline = []
for row in table:
mlrow = []
for col,cell in enumerate(row):
# If this cell exceeds its max width, put it on multiple lines
if widths is not None and \
widths[col] is not None and \
len(cell) > widths[col]:
wrapper.width = widths[col]
lines = []
# Split on manual line breaks in the input as well
for input_line in cell.split("\n"):
lines.extend(wrapper.wrap(input_line))
else:
lines = cell.split("\n")
mlrow.append(lines)
multiline.append(mlrow)
for row in multiline:
if outer_seps:
print >> out, separator,
# Find out the cell with the most lines in this row
max_lines = max(len(cell) for cell in row)
# Each line of the row
for line in range(max_lines):
for col in range(len(row)):
# If this cell doesn't have this many lines, just pad
padsize = col_paddings[col] + 2
if line >= len(row[col]):
text = " " * padsize
else:
# There's text: justify it
if coljusts[col]:
text = row[col][line].ljust(padsize)
else:
text = row[col][line].rjust(padsize)
if col != 0 and separator:
print >> out, separator,
print >> out, text,
#.........这里部分代码省略.........