使用 textwrap 在 Python 中包裝、截斷和格式化字符串

商業

要在 Python 中通過換行(換行)和截斷(縮寫)任意數量的字符來格式化字符串,請使用標準庫的 textwrap 模塊。

此處提供了以下信息。

  • 換行(換行):wrap(),fill()
  • 截斷字符串(省略):shorten()
  • TextWrapper 對象

如果您想在代碼中而不是在輸出中編寫多行長字符串,請參閱以下文章。

換行(換行):wrap(),fill()

使用 textwrap 模塊的 wrap() 函數,您可以獲得一個除以分詞符的列表,以適應任意數量的字符。

指定第二個參數寬度的字符數。默認值為寬度 = 70。

import textwrap

s = "Python can be easy to pick up whether you're a first time programmer or you're experienced with other languages"

s_wrap_list = textwrap.wrap(s, 40)
print(s_wrap_list)
# ['Python can be easy to pick up whether', "you're a first time programmer or you're", 'experienced with other languages']

使用獲得的列表,您可以通過執行以下操作來獲取被換行代碼破壞的字符串
\n'.join(list)

print('\n'.join(s_wrap_list))
# Python can be easy to pick up whether
# you're a first time programmer or you're
# experienced with other languages

函數 fill() 返回一個換行符字符串而不是一個列表。與上例中在 wrap() 之後執行以下代碼相同。
\n'.join(list)

當您不需要列表但想將固定寬度的字符串輸出到終端等時,這會更方便。

print(textwrap.fill(s, 40))
# Python can be easy to pick up whether
# you're a first time programmer or you're
# experienced with other languages

如果指定了參數 max_line,則將省略其後的行數。

print(textwrap.wrap(s, 40, max_lines=2))
# ['Python can be easy to pick up whether', "you're a first time programmer or [...]"]

print(textwrap.fill(s, 40, max_lines=2))
# Python can be easy to pick up whether
# you're a first time programmer or [...]

如果省略,則默認在末尾輸出以下字符串。
[...]'

它可以被任何帶有參數佔位符的字符串替換。

print(textwrap.fill(s, 40, max_lines=2, placeholder=' ~'))
# Python can be easy to pick up whether
# you're a first time programmer or ~

您還可以使用參數 initial_indent 指定要添加到第一行開頭的字符串。當您想要縮進段落的開頭時,可以使用它。

print(textwrap.fill(s, 40, max_lines=2, placeholder=' ~', initial_indent='  '))
#   Python can be easy to pick up whether
# you're a first time programmer or ~

小心全角和半角字符。

在 textwrap 中,字符數由字符數控制,而不是由字符寬度控制,單字節和雙字節字符都被視為一個字符。

s = '文字文字文字文字文字文字12345,67890, 文字文字文字abcde'

print(textwrap.fill(s, 12))
# 文字文字文字文字文字文字
# 12345,67890,
# 文字文字文字abcde

如果您想用固定寬度的混合漢字字符換行文本,請參考以下內容。

截斷字符串(省略):shorten()

如果要截斷和省略字符串,請使用 textwrap 模塊中的函數 short()。

以單詞為單位縮寫以適應任意數量的字符。包括表示省略的字符串在內的字符數是任意的。表示省略的字符串可以用參數佔位符設置,默認如下。
[...]'

s = 'Python is powerful'

print(textwrap.shorten(s, 12))
# Python [...]

print(textwrap.shorten(s, 12, placeholder=' ~'))
# Python is ~

但是,例如,日語字符串不能很好地縮寫,因為它們不能分成單詞。

s = 'Pythonについて。Pythonは汎用のプログラミング言語である。'

print(textwrap.shorten(s, 20))
# [...]

如果你想通過只考慮字符數而不是單詞單位來縮寫,可以很容易地實現如下。

s_short = s[:12] + '...'
print(s_short)
# Pythonについて。P...

TextWrapper 對象

如果要使用固定配置多次 wrap() 或 fill(),則創建 TextWrapper 對象會很有效。

wrapper = textwrap.TextWrapper(width=30, max_lines=3, placeholder=' ~', initial_indent='  ')

s = "Python can be easy to pick up whether you're a first time programmer or you're experienced with other languages"

print(wrapper.wrap(s))
# ['  Python can be easy to pick', "up whether you're a first time", "programmer or you're ~"]

print(wrapper.fill(s))
#   Python can be easy to pick
# up whether you're a first time
# programmer or you're ~

可以重複使用相同的設置。