textwrap文本格式化
fill函数
输出文字并要求格式文本
widen限制文本宽度
textwrap.fill(dedented_text, width=width)
悬挂缩进
dedented_text = textwrap.dedent(sample_text).strip()
print(textwrap.fill(dedented_text,
initial_indent='',
subsequent_indent=' ' * 4,
width=50,
))
dedent函数
输出结果是一段删除了每一行中都存在的缩进空白的文字,如果一行比其他缩进更多,多出的部分将不会被移除
常配合strip()函数使用
dedented_text = textwrap.dedent(sample_text)
print('Dedented:')
print(dedented_text)
#output
The textwrap module can be used to format text for output in
situations where pretty-printing is desired. It offers
programmatic functionality similar to the paragraph wrapping
or filling features found in many text editors.
indent函数
每一行开头加入前缀文本
final = textwrap.indent(wrapped, 'EVEN ',predicate=should_indent)
indent(文本名,’添加值‘,判断值=1添加)
final = textwrap.indent(wrapped, '>)#每行文字前面添加>
shorten函数
textwrap.shorten(text, width,placeholder=?)
text文件名
widen保留文本宽度
placeholder=?改变替换位置的占位符