python:textwrap --文字自動換行與填充
textwrap
模組提供了一些快捷函式,以及可以完成所有工作的類 TextWrapper
。 如果你只是要對一兩個文字字串進行自動換行或填充,快捷函式應該就夠用了;否則的話,你應該使用 TextWrapper
的例項來提高效率。
語法簡介
textwrap
模組提供的快捷函式如下:
wrap函式:
- 對 text (字串) 中的單獨段落自動換行以使每行長度最多為 width 個字元。 返回由輸出行組成的列表,行尾不帶換行符。
textwrap.wrap(text,width=70,*,initial_indent='',subsequent_indent='',expand_tabs=True,replace_whitespace=True,fix_sentence_endings=False,break_long_words=True,drop_whitespace=True,break_on_hyphens=True,tabsize=8,max_lines=None)
fill函式:
- 對 text 中的單獨段落自動換行,並返回一個包含被自動換行段落的單獨字串。
textwrap.fill(text,width=70,*,initial_indent='',subsequent_indent='',expand_tabs=True,replace_whitespace=True,fix_sentence_endings=False,break_long_words=True,drop_whitespace=True,break_on_hyphens=True,tabsize=8,max_lines=None)
shorten函式:
- 摺疊並截短給定的 text 以符合給定的 width。
textwrap.shorten(text,width=70,*,initial_indent='',subsequent_indent='',expand_tabs=True,replace_whitespace=True,fix_sentence_endings=False,break_long_words=True,drop_whitespace=True,break_on_hyphens=True,tabsize=8,max_lines=None)
dedent函式:
- 移除 text 中每一行的任何相同字首空白符。
textwrap.shorten(text)
indent函式:
- 將 prefix 新增到 text 中選定行的開頭。
- 通過呼叫 text.splitlines(True) 來對行進行拆分。
- 預設情況下,prefix 會被新增到所有不是隻由空白符(包括任何行結束符)組成的行。
textwrap.indent(text,prefix,predicate=None)
引數說明:
-
width:(預設: 70) 自動換行的最大行長度。
-
expand_tabs:(預設: True) 如果為真值,則 text 中所有的製表符將使用 text 的 expandtabs() 方法擴充套件為空格符。
-
tabsize:(預設: 8) 如果 expand_tabs 為真值,則 text 中所有的製表符將擴充套件為零個或多個空格,具體取決於當前列位置和給定的製表寬度。
-
replace_whitespace:(預設: True) 如果為真值,在製表符擴充套件之後、自動換行之前,wrap() 方法將把每個空白字元都替換為單個空格。 會被替換的空白字元如下:製表,換行,垂直製表和回車 ('\t\n\v\r')。
-
drop_whitespace:(預設: True) 如果為真值,每一行開頭和末尾的空白字元(在包裝之後、縮排之前)會被丟棄。 但是段落開頭的空白字元如果後面不帶任何非空白字元則不會被丟棄。 如果被丟棄的空白字元佔據了一個整行,則該整行將被丟棄。
-
initial_indent:(預設: ' ') 將被新增到被自動換行輸出內容的第一行的字串。 其長度會被計入第一行的長度。 空字串不會被縮排。
-
subsequent_indent:(預設: ' ') 將被新增到被自動換行輸出內容除第一行外的所有行的字串。 其長度會被計入除行一行外的所有行的長度。
-
fix_sentence_endings:(預設: False) 如果為真值,TextWrapper 將嘗試檢測句子結尾並確保句子間總是以恰好兩個空格符分隔。 對於使用等寬字型的文字來說通常都需要這樣。
-
break_long_words:(預設: True) 如果為真值,則長度超過 width 的單詞將被分開以保證行的長度不會超過 width。 如果為假值,超長單詞不會被分開,因而某些行的長度可能會超過 width。 (
-
break_on_hyphens:(預設: True) 如果為真值,將根據英語的慣例首選在空白符和複合詞的連字元之後自動換行。 如果為假值,則只有空白符會被視為合適的潛在斷行位置,但如果你確實不希望出現分開的單詞則你必須將
break_long_words 設為假值。 -
max_lines:(預設: None) 如果不為 None,則輸出內容將最多包含 max_lines 行,並使 placeholder 出現在輸出內容的末尾。
-
placeholder:(預設: ' […]') 該文字將在輸出文字被截短時出現在文字末尾。
示例展示
wrap
函式的示例如下:
importtextwrap
str_data="""
哈哈\t哈啊哈哈\t啊哈哈哈啊哈哈哈懷舊服翻山倒海不會發生代表/n會盡快發貨時.?electroencephalography
"""
print(str_data)
print(textwrap.wrap(str_data,width=5))
print(textwrap.wrap(str_data,initial_indent='&&'))
print(textwrap.wrap(str_data,subsequent_indent='10'))
print(textwrap.wrap(str_data,expand_tabs=False))
print(textwrap.wrap(str_data,tabsize=5))
print(textwrap.wrap(str_data,width=5,replace_whitespace=False))
print(textwrap.wrap(str_data,width=5,fix_sentence_endings=True))
print(textwrap.wrap(str_data,width=5,break_long_words=False))
print(textwrap.wrap(str_data,width=5,break_on_hyphens=True))
print(textwrap.wrap(str_data,width=5,drop_whitespace=True))
print(textwrap.wrap(str_data,width=5,max_lines=1,placeholder='》》》》》'))
執行結果
哈哈哈啊哈哈啊哈哈哈啊哈哈哈懷舊服翻山倒海不會發生代表/n會盡快發貨時.?electroencephalography
['哈哈','哈啊哈哈','啊哈哈哈啊','哈哈哈','懷舊服','翻山倒海','不會發生代','表/n會盡','快發貨時.','?elec','troen','cepha','logra','phy']
['&&哈哈哈啊哈哈啊哈哈哈啊哈哈哈懷舊服翻山倒海','不會發生代表/n會盡快發貨時.?electroencephalography']
['哈哈哈啊哈哈啊哈哈哈啊哈哈哈懷舊服翻山倒海','10不會發生代表/n會盡快發貨時.?electroencephalography']
['哈哈哈啊哈哈啊哈哈哈啊哈哈哈懷舊服翻山倒海','不會發生代表/n會盡快發貨時.?electroencephalography']
['哈哈哈啊哈哈啊哈哈哈啊哈哈哈懷舊服翻山倒海','不會發生代表/n會盡快發貨時.?electroencephalography']
['\n哈哈','哈啊哈哈','啊哈哈哈啊','哈哈哈','懷舊服','翻山倒海','不會發生代','表/n會盡','快發貨時.','?elec','troen','cepha','logra','phy']
['哈哈','哈啊哈哈','啊哈哈哈啊','哈哈哈','懷舊服','翻山倒海','不會發生代','表/n會盡','快發貨時.','?elec','troen','cepha','logra','phy']
['哈哈','哈啊哈哈','啊哈哈哈啊哈哈哈','懷舊服','翻山倒海','不會發生代表/n會盡快發貨時.?electroencephalography']
['哈哈','哈啊哈哈','啊哈哈哈啊','哈哈哈','懷舊服','翻山倒海','不會發生代','表/n會盡','快發貨時.','?elec','troen','cepha','logra','phy']
['哈哈','哈啊哈哈','啊哈哈哈啊','哈哈哈','懷舊服','翻山倒海','不會發生代','表/n會盡','快發貨時.','?elec','troen','cepha','logra','phy']
['》》》》》']
Processfinishedwithexitcode0
fill
函式的示例如下:
fill
函式和wrap
函式都是共用TextWrapper
類實現的,兩個函式唯一的區別就是,wrap
函式返回的是列表型別,fill
函式返回的是字串型別
shorten
函式的示例如下:
shorten
函式和fill
函式、wrap
函式的區別就是它的width
引數是必傳的,最後返回的都是摺疊的資料
importtextwrap
str_data="""
哈哈\t哈啊哈哈\t啊哈哈哈啊哈哈哈懷舊服翻山倒海不會發生代表/n會盡快發貨時.?electroencephalography
"""
print(str_data)
print(textwrap.shorten(str_data,width=5))
print(textwrap.shorten(str_data,width=10,initial_indent='&&'))
print(textwrap.shorten(str_data,width=5,subsequent_indent='10'))
print(textwrap.shorten(str_data,width=5,expand_tabs=False))
print(textwrap.shorten(str_data,width=5,tabsize=5))
print(textwrap.shorten(str_data,width=5,replace_whitespace=False))
print(textwrap.shorten(str_data,width=5,fix_sentence_endings=True))
print(textwrap.shorten(str_data,width=5,break_long_words=False))
print(textwrap.shorten(str_data,width=5,break_on_hyphens=True))
print(textwrap.shorten(str_data,width=5,drop_whitespace=True))
print(textwrap.shorten(str_data,width=5,placeholder='...'))
執行結果
哈哈哈啊哈哈啊哈哈哈啊哈哈哈懷舊服翻山倒海不會發生代表/n會盡快發貨時.?electroencephalography
[...]
&&哈哈[...]
[...]
[...]
[...]
[...]
[...]
[...]
[...]
[...]
哈哈...
Processfinishedwithexitcode0
dedent
函式的示例如下:
- 移除 text 中每一行的任何相同字首空白符
importtextwrap
str_data="""
哈哈\t哈啊哈哈\t啊哈哈哈啊哈哈哈懷舊服翻山倒海不會發生代表/n會盡快發貨時.?electroencephalography
"""
print(str_data)
print(textwrap.dedent(str_data))
執行結果
哈哈哈啊哈哈啊哈哈哈啊哈哈哈懷舊服翻山倒海不會發生代表/n會盡快發貨時.?electroencephalography
哈哈哈啊哈哈啊哈哈哈啊哈哈哈懷舊服翻山倒海不會發生代表/n會盡快發貨時.?electroencephalography
dedent
函式的示例如下:
- 將 prefix 新增到 text 中選定行的開頭
importtextwrap
str_data="""
/n/n哈哈\t哈啊哈哈\t啊哈哈哈啊哈哈哈懷舊服翻山倒海不會發生代表/n會盡快發貨時.?electroencephalography
"""
print(str_data)
print(textwrap.indent(str_data,prefix=''))
執行結果
/n/n哈哈哈啊哈哈啊哈哈哈啊哈哈哈懷舊服翻山倒海不會發生代表/n會盡快發貨時.?electroencephalography
/n/n哈哈哈啊哈哈啊哈哈哈啊哈哈哈懷舊服翻山倒海不會發生代表/n會盡快發貨時.?electroencephalography
Processfinishedwithexitcode0
- 可選的 predicate 引數可用來控制哪些行要縮排。 例如,可以很容易地為空行或只有空白符的行新增 prefix:
importtextwrap
str_data="""
哈哈哈啊\n哈哈啊哈哈哈\n啊哈哈哈懷舊服\n翻山\n倒海不會發生代表\n會盡快發貨時.?electroencephalography
"""
print(str_data)
print("_"*50)
print(textwrap.indent(str_data,'+'))
print("_"*50)
print(textwrap.indent(str_data,'+',lambdaline:True))
執行結果
哈哈哈啊
哈哈啊哈哈哈
啊哈哈哈懷舊服
翻山
倒海不會發生代表
會盡快發貨時.?electroencephalography
__________________________________________________
+哈哈哈啊
+哈哈啊哈哈哈
+啊哈哈哈懷舊服
+翻山
+倒海不會發生代表
+會盡快發貨時.?electroencephalography
__________________________________________________
+
+哈哈哈啊
+哈哈啊哈哈哈
+啊哈哈哈懷舊服
+翻山
+倒海不會發生代表
+會盡快發貨時.?electroencephalography
Processfinishedwithexitcode0
以上總結或許能幫助到你,或許幫助不到你,但還是希望能幫助到你,如有疑問、歧義,直接私信留言會及時修正釋出;非常期待你的點贊和分享喲,謝謝!
未完,待續…
一直都在努力,希望您也是!
微信搜尋公眾號:就用python
文章作者:李 鋒|編輯排版:樑莉莉