textwrap.fill(text, width=70, **kwargs)
格式化文本,跟函數wrap的功能1樣,只不過它返回的不是列表方式,而1段連接1起的文本。可以認為等價于下面的代碼:
“ ”.join(wrap(text, ...))
參數與wrap參數是1樣的功能。
例子:
#python 3.4.3
import textwrap
text = this for test!
format = textwrap.fill(text, 10)
print(format)
text =
You say that you love the rain, but you open your umbrella when it rains.
You say that you love the sun, but you find a shadow spot when the sun shines.
You say that you love the wind, but you close your windows when wind blows.
This is why I am afraid, when you say that you love me too.
format = textwrap.fill(text, 30)
print(format)
結果輸出以下:
this for
test!
You say that you
love the rain, but you open
your umbrella when it rains.
You say that you love the sun,
but you find a shadow spot
when the sun shines. You say
that you love the wind, but
you close your windows when
wind blows. This is why I am
afraid, when you say that you
love me too.
textwrap.shorten(text, width, **kwargs)
根據給出的行寬度進行格式化,當行寬度大于文本實際長度時,完全輸出;當行寬度小于文本實際長度時,只輸出部份頭部文本,和后去掉的文本以省略號的方式顯示。
參數text是要格式化的文本。
參數width是行寬度。
參數kwargs是關鍵字參數。
例子:
#python 3.4.3
import textwrap
text = this for test!
format = textwrap.shorten(text, width=12)
print(format)
format = textwrap.shorten(text, width=100)
print(format)
format = textwrap.shorten(text, width=10, placeholder="...")
print(format)
輸出結果以下:
this [...]
this for test!
this...
textwrap.dedent(text)
把每行文本前面的縮進空格進行去除掉。
例子:
#python 3.4.3
import textwrap
text =
1. this for test!
2. abc
3. shenzhen
print(text)
print(不要每行縮進:)
format = textwrap.dedent(text)
print(format)
結果輸出以下:
>>>
1. this for test!
2. abc
3. shenzhen
不要每行縮進:
1. this for test!
2. abc
3. shenzhen
>>>
textwrap.indent(text, prefix, predicate=None)
把文本每行進行縮進,縮進前綴可使用prefix定義。
參數text是要縮進的文本。
參數prefix是縮進輸出的字符串,可使用空格等等。
參數predicate是控制每行是不是縮進的lambda函數。
例子:
#python 3.4.3
import textwrap
text =
1. this for test!
2. abc
3. shenzhen
print(text)
print(每行增加縮進:)
format = textwrap.indent(text, )
print(format)
print(lambda:)
format = textwrap.indent(text, 小蔡說:,
lambda x: True if len(x) > 10 else False)
print(format)
結果輸出以下:
1. this for test!
2. abc
3. shenzhen
每行增加縮進:
1. this for test!
2. abc
3. shenzhen
lambda:
小蔡說:1. this for test!
2. abc
小蔡說:3. shenzhen
class textwrap.TextWrapper(**kwargs)
TextWrapper類的構造函數,構造1個處理文本格式化的對象。
參數kwargs是關鍵字參數,可以設置下面說明的關鍵字參數:
width
每行最大的長度,超過此長度就進行換行。默許是70個字符。
expand_tabs
如果本標志為True,在進行文本填充操作時把跳格鍵使用expandtabs()函數擴大文本,反之不進行這個操作。默許是True。
tabsize
如果expand_tabs為True,在填充時按tabsize設置的大小來填充。默許為8個空格字符。
replace_whitespace
如果expand_tabs為True,此標志不起作用。如果expand_tabs為False,會把字符集合( vf )每一個替換為1個空格字符,而不作擴大。
drop_whitespace
如果此標志設置為True,在格式化之前的每行字符的行頭和行尾的空格都會被刪除掉。
initial_indent
在第1行的行首添加指定的字符顯示。默許是空白。如果首行是空白行就不會添加。
subsequent_indent
除第1行,所有其它行都在行首添加這個字符串的輸出。默許為空。
fix_sentence_endings
當此標志為True時,表示檢測到句尾時添加固定兩個空格在句尾,以便下1句不緊挨著上1句。默許為False。
break_long_words
當此標志為True時,如果1句話后面的單詞超過設置行寬度(width),會自動切斷這個單詞,以便滿足行寬度的長度要求。如果為False,就不切斷,可能有些行就會超過行寬度的要求。
break_on_hyphens
如果此標志為True,復合詞之間連字符可以視為換行的字符。如果設置為False,就不允許。默許為True。
max_lines
如果此變量定義了最多輸出多少行,如果文本輸出超過設置的行,就輸出省略標志[...]。默許此變量為None,所有內容全部輸出。
placeholder
如果文本已輸出到達限制長度,就會去掉,并在此位置輸出placeholder字符串。默許此字符串為[...]。用戶可自己定義此字符串。
TextWrapper.wrap(text)
對1段文本進行換行等格式化,返回字符串行列表的方式。
TextWrapper.fill(text)
對1段文本進行換行等格式化返回字符串方式。
例子:
#python 3.4.3
import textwrap
text =
1. this for test!
2.abc
3. shenzhen
print(text)
print(每行增加縮進:)
txtwr = textwrap.TextWrapper(width = 10, expand_tabs = False,
tabsize = 3, replace_whitespace = True,
initial_indent = ###,
max_lines = 2)
format = txtwr.fill(text)
print(format)
結果輸出:
1. this for test!
2.abc
3. shenzhen
每行增加縮進:
###1. this
for [...]