日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > php教程 > 3.4 textwrap--格式化文本庫

3.4 textwrap--格式化文本庫

來源:程序員人生   發布時間:2016-04-20 10:39:39 閱讀次數:2426次

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_tabsTrue,在填充時按tabsize設置的大小來填充。默許為8個空格字符。

replace_whitespace

如果expand_tabsTrue,此標志不起作用。如果expand_tabsFalse,會把字符集合( 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 [...]

生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 日韩电影av在线 | 成年人在线看片 | 成人性生交大片免费看视频r | 国产伦精品一区二区三区照片91 | 亚洲视频在线视频 | 免费视频一二三区 | 午夜伊人 | 精久国产一区二区三区四区 | 久热久| 成人国产一区 | 日韩一级黄色 | 欧美成人小视频 | 国产香蕉精品视频 | 福利视频一区二区三区 | 久久曰视频 | 九九热在线观看 | 欧美在线一区二区三区 | a在线资源 | 欧美日韩激情一区 | av在线资源网 | 亚洲欧洲无码一区二区三区 | 视频在线二区 | 国产精品免费视频观看 | 久久久久久国产 | 999re5这里只有精品 | www.国产精| 欧美不卡在线 | 国产一区二区三区视频在线 | 99re8在线精品视频免费播放 | 日本精品在线 | 国产在线精品一区二区三区 | 黄色一级视频免费看 | 国产精品不卡 | 久久久91精品 | 一区二区三区在线播放 | 国产精品影院在线观看 | 亚洲黄色免费看 | 久久久综合精品 | 天天色综合天天色 | 亚洲欧洲av| 欧美一区二区久久久 |