當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python textwrap.shorten用法及代碼示例

用法:

textwrap.shorten(text, width, *, fix_sentence_endings=False, break_long_words=True, break_on_hyphens=True, placeholder=' [...]')

折疊並截斷給定的 text 以適應給定的 width

首先 text 中的空格被折疊(所有空格都被單個空格替換)。如果結果適合 width ,則返回。否則,從末尾刪除足夠的單詞,以便剩餘的單詞加上 placeholder 適合 width

>>> textwrap.shorten("Hello  world!", width=12)
'Hello world!'
>>> textwrap.shorten("Hello  world!", width=11)
'Hello [...]'
>>> textwrap.shorten("Hello world", width=10, placeholder="...")
'Hello...'

可選關鍵字參數對應於 TextWrapper 的實例屬性,如下所述。請注意,在將文本傳遞給 TextWrapper fill() 函數之前,空格已折疊,因此更改 tabsizeexpand_tabsdrop_whitespacereplace_whitespace 的值將無效。

3.4 版中的新函數。

相關用法


注:本文由純淨天空篩選整理自python.org大神的英文原創作品 textwrap.shorten。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。