textwrap

 

2022-12-11

import:: import textwrap

doc:: textwrap

desc:: 文本自动换行与填充


textwrap.fill()

主要是可以指定字符换行

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

textwrap.dedent()

去除行前空格

1
textwrap.dedent(str)

textwrap.indent()

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

1
textwrap.indent(str, ">")

textwrap.shorten()

长文本截断

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

中文分词可使用jieba