textwrap.fill(text, width=70, **kwargs)
格式化文本,跟函數(shù)wrap的功能1樣,只不過它返回的不是列表方式,而1段連接1起的文本。可以認(rèn)為等價(jià)于下面的代碼:
“ ”.join(wrap(text, ...))
參數(shù)與wrap參數(shù)是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)
結(jié)果輸出以下:
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)
根據(jù)給出的行寬度進(jìn)行格式化,當(dāng)行寬度大于文本實(shí)際長度時(shí),完全輸出;當(dāng)行寬度小于文本實(shí)際長度時(shí),只輸出部份頭部文本,和后去掉的文本以省略號的方式顯示。
參數(shù)text是要格式化的文本。
參數(shù)width是行寬度。
參數(shù)kwargs是關(guān)鍵字參數(shù)。
例子:
#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)
輸出結(jié)果以下:
this [...]
this for test!
this...
textwrap.dedent(text)
把每行文本前面的縮進(jìn)空格進(jìn)行去除掉。
例子:
#python 3.4.3
import textwrap
text =
1. this for test!
2. abc
3. shenzhen
print(text)
print(不要每行縮進(jìn):)
format = textwrap.dedent(text)
print(format)
結(jié)果輸出以下:
>>>
1. this for test!
2. abc
3. shenzhen
不要每行縮進(jìn):
1. this for test!
2. abc
3. shenzhen
>>>
textwrap.indent(text, prefix, predicate=None)
把文本每行進(jìn)行縮進(jìn),縮進(jìn)前綴可使用prefix定義。
參數(shù)text是要縮進(jìn)的文本。
參數(shù)prefix是縮進(jìn)輸出的字符串,可使用空格等等。
參數(shù)predicate是控制每行是不是縮進(jìn)的lambda函數(shù)。
例子:
#python 3.4.3
import textwrap
text =
1. this for test!
2. abc
3. shenzhen
print(text)
print(每行增加縮進(jìn):)
format = textwrap.indent(text, )
print(format)
print(lambda:)
format = textwrap.indent(text, 小蔡說:,
lambda x: True if len(x) > 10 else False)
print(format)
結(jié)果輸出以下:
1. this for test!
2. abc
3. shenzhen
每行增加縮進(jìn):
1. this for test!
2. abc
3. shenzhen
lambda:
小蔡說:1. this for test!
2. abc
小蔡說:3. shenzhen
class textwrap.TextWrapper(**kwargs)
TextWrapper類的構(gòu)造函數(shù),構(gòu)造1個(gè)處理文本格式化的對象。
參數(shù)kwargs是關(guān)鍵字參數(shù),可以設(shè)置下面說明的關(guān)鍵字參數(shù):
width
每行最大的長度,超過此長度就進(jìn)行換行。默許是70個(gè)字符。
expand_tabs
如果本標(biāo)志為True,在進(jìn)行文本填充操作時(shí)把跳格鍵使用expandtabs()函數(shù)擴(kuò)大文本,反之不進(jìn)行這個(gè)操作。默許是True。
tabsize
如果expand_tabs為True,在填充時(shí)按tabsize設(shè)置的大小來填充。默許為8個(gè)空格字符。
replace_whitespace
如果expand_tabs為True,此標(biāo)志不起作用。如果expand_tabs為False,會(huì)把字符集合( vf )每一個(gè)替換為1個(gè)空格字符,而不作擴(kuò)大。
drop_whitespace
如果此標(biāo)志設(shè)置為True,在格式化之前的每行字符的行頭和行尾的空格都會(huì)被刪除掉。
initial_indent
在第1行的行首添加指定的字符顯示。默許是空白。如果首行是空白行就不會(huì)添加。
subsequent_indent
除第1行,所有其它行都在行首添加這個(gè)字符串的輸出。默許為空。
fix_sentence_endings
當(dāng)此標(biāo)志為True時(shí),表示檢測到句尾時(shí)添加固定兩個(gè)空格在句尾,以便下1句不緊挨著上1句。默許為False。
break_long_words
當(dāng)此標(biāo)志為True時(shí),如果1句話后面的單詞超過設(shè)置行寬度(width),會(huì)自動(dòng)切斷這個(gè)單詞,以便滿足行寬度的長度要求。如果為False,就不切斷,可能有些行就會(huì)超過行寬度的要求。
break_on_hyphens
如果此標(biāo)志為True,復(fù)合詞之間連字符可以視為換行的字符。如果設(shè)置為False,就不允許。默許為True。
max_lines
如果此變量定義了最多輸出多少行,如果文本輸出超過設(shè)置的行,就輸出省略標(biāo)志[...]。默許此變量為None,所有內(nèi)容全部輸出。
placeholder
如果文本已輸出到達(dá)限制長度,就會(huì)去掉,并在此位置輸出placeholder字符串。默許此字符串為[...]。用戶可自己定義此字符串。
TextWrapper.wrap(text)
對1段文本進(jìn)行換行等格式化,返回字符串行列表的方式。
TextWrapper.fill(text)
對1段文本進(jìn)行換行等格式化返回字符串方式。
例子:
#python 3.4.3
import textwrap
text =
1. this for test!
2.abc
3. shenzhen
print(text)
print(每行增加縮進(jìn):)
txtwr = textwrap.TextWrapper(width = 10, expand_tabs = False,
tabsize = 3, replace_whitespace = True,
initial_indent = ###,
max_lines = 2)
format = txtwr.fill(text)
print(format)
結(jié)果輸出:
1. this for test!
2.abc
3. shenzhen
每行增加縮進(jìn):
###1. this
for [...]