普贤教程 › 包参考
textwrap 包
文本排版(换行/缩进/去缩进)
| 项 | 值 |
|---|---|
| 包名 | textwrap |
| 版本 | 0.2.0 |
| 可用版本 | 0.1.0 / 0.2.0 |
| 入口 | textwrap.px |
| 分类 | 文本与中文处理 |
| 文件数 / 行数 | 1 / 304 |
安装与引入
bash
pxpkg init # 首次 pxpkg add textwrap@0.2.0 pxpkg install
px
import textwrap所属分类教程:文本与中文处理
公开接口
| 函数 | 参数 |
|---|---|
tw_wrap | s, width |
tw_fill | s, width |
tw_dedent | s |
tw_indent | s, prefix |
其余 13 个 def(内部与辅助,可直接调用但不属稳定接口)
| 函数 | 参数 |
|---|---|
tw_split_words | s |
tw_center | s, width, fillchar = " " |
tw_ljust | s, width, fillchar = " " |
tw_rjust | s, width, fillchar = " " |
tw_strip_lines | s |
tw_shorten | s, width, placeholder = "..." |
tw_split_paragraphs | s |
tw_line_count | s |
tw_is_cjk | c |
tw_word_count | s |
tw_expandtabs | s, tabsize = 8 |
tw_rstrip_lines | s |
tw_fill_indent | s, width, initial_indent = "", subsequent_indent = "" |
包内说明(源码头注释)
text
============================================================ 普贤生态库:registry/textwrap —— 文本排版(换行/缩进/去缩进) ------------------------------------------------------------ 纯函数库(无 IO、无状态),编译(px build)与解释(px run)双模式一致。 tw_wrap(s, width) → [str] 按词断行(默认词间单空格,空行保留) tw_fill(s, width) → str wrap 后用 "\n" 连接 tw_dedent(s) → str 去除所有行的公共前导空白 tw_indent(s, prefix)→ str 给非空行加前缀 说明: - width 按 rune 计数(len(s),中文每字算 1;实际显示宽度约 2 为排版子集) ============================================================ 内部:按 " " 分词