import:: import textwrap doc:: textwrap desc:: 文本自动换行与填充


textwrap.fill()

主要是可以指定字符换行

textwrap.fill(
              str,
              width=42, # 最大框度
              initial_indent=" " # 首行缩进
              subsequent_indent="" # 非首行缩进(前缀)
 )

textwrap.dedent()

去除行前空格

textwrap.dedent(str)

textwrap.indent()

为每一行自动增加缩进或是指定符号,同样也可以使用 fill 中的 subsequent_indent 实现。

textwrap.indent(str, ">")

textwrap.shorten()

长文本截断

textwrap.shorten(
                 str,
                 width=10,         # 指定长度后截断
                 placeholder="..." # 截断占位符,默认值是 [...]
 )
# 返回: 'Hello...'

中文分词可使用jieba