zl程序教程

您现在的位置是:首页 >  其它

当前栏目

3.4 textwrap--格式化文本库(2)

-- 格式化 3.4
2023-09-14 09:10:34 时间

textwrap.fill(text, width=70, **kwargs)

格式化文本,跟函数wrap的功能一样,只不过它返回的不是列表方式,而一段连接一起的文本。可以认为等价于下面的代码:

“\n”.join(wrap(text, ...))

参数与wrap参数是一样的功能。

例子:

#python 3.4.3

 

import textwrap

 

text = 'this for test!'

format = textwrap.fill(text, 10)

print(format)

 

text = '''<I am afraid>

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!

<I am afraid> 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类的构造函数,构造一个处理文本格式化的对象。

参数kwargs是关键字参数,可以设置下面说明的关键字参数:

width

每行最大的长度,超过此长度就进行换行。默认是70个字符。

expand_tabs

如果本标志为True,在进行文本填充操作时把跳格键使用expandtabs()函数扩展文本,反之不进行这个操作。默认是True

tabsize

如果expand_tabsTrue,在填充时按tabsize设置的大小来填充。默认为8个空格字符。

replace_whitespace

如果expand_tabsTrue,此标志不起作用。如果expand_tabsFalse,会把字符集合(\t\n\v\f\r)每个替换为一个空格字符,而不作扩展。

drop_whitespace

如果此标志设置为True,在格式化之前的每行字符的行头和行尾的空格都会被删除掉。

initial_indent

在第一行的行首添加指定的字符显示。默认是空白。如果首行是空白行就不会添加。

subsequent_indent

除了第一行,所有其它行都在行首添加这个字符串的输出。默认为空。

fix_sentence_endings

当此标志为True时,表示检测到句尾时添加固定两个空格在句尾,以便下一句不紧挨着上一句。默认为False

break_long_words

当此标志为True时,如果一句话后面的单词超过设置行宽度(width),会自动切断这个单词,以便满足行宽度的长度要求。如果为False,就不切断,可能有些行就会超过行宽度的要求。

break_on_hyphens

如果此标志为True,复合词之间连字符可以视为换行的字符。如果设置为False,就不允许。默认为True

max_lines

如果此变量定义了最多输出多少行,如果文本输出超过设置的行,就输出省略标志[...]。默认此变量为None,所有内容全部输出。

placeholder

如果文本已经输出达到限制长度,就会去掉,并在此位置输出placeholder字符串。默认此字符串为[...]。用户可自己定义此字符串。

TextWrapper.wrap(text)

对一段文本进行换行等格式化,返回字符串行列表的方式。

TextWrapper.fill(text)

对一段文本进行换行等格式化返回字符串方式。

 

例子:

#python 3.4.3

 

import textwrap

 

text = '''\

1. this for test!

2.abc

3.\t\tshenzhen'''

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 [...]

 


蔡军生  QQ:9073204  深圳