textwrap模块的函数接口

2020年1月4日 / 17次阅读 / Last Modified 2020年5月14日
字符串

python内置的textwrap模块,可以很方便的用来处理大块的text文本。本文努力通过实例来介绍这个模块的几个函数接口的使用。

textwrap.wrap函数

textwrap.wrap函数,一大段文本按照固定的宽度进行分割,默认宽度是70(字符),此函数返回一个list,list中每个元素为按照宽度分割的字符串。

>>> f = open('lynch.txt')
>>> tt = f.read()
>>> import textwrap
>>> textwrap.wrap(tt)
['My best stocks have been ones where I didn’t have to worry about the', 'big picture. A company with a better mousetrap, a growth company in a', 'non-growth industry. Stop & Shop and Dunkin’ Donuts are two incredibly', 'successful local companies.  The question is, how long is the story? I', 'wish I had been to Arkansas and gone to see Sam Walton of Walmart. Ten', 'years after Walmart went public, it was a 25-year-old company. It was', 'up tenfold. I said, “Ooh, I missed that one.” Then it went up 50-fold.', 'Sherwin Williams earnings are up 20-fold since I managed the stock in', 'Magellan. The professional painter in our little town goes to Sherwin', 'Williams, not to Home Depot. Why didn’t I look at Sherwin Williams?', 'Why didn’t I spend an hour on that? So staggering, dumb, or just lazy.', 'I thought it was the last inning of the ballgame, without doing the', 'research.  You can’t make these conclusions without some basis. Really', 'bad American fast food has done brilliantly overseas. There are 1,400', 'McDonald’s in France. You want to be in [the stock] in the second', 'inning of the ballgame, and out in the seventh. That could be 30', 'years. Like the people who were really wrong on McDonald’s. They', 'thought they were near the end, and they forgot about the other seven', 'billion people in the world.']
>>> for i in textwrap.wrap(tt): print(i)
...
My best stocks have been ones where I didn’t have to worry about the
big picture. A company with a better mousetrap, a growth company in a
non-growth industry. Stop & Shop and Dunkin’ Donuts are two incredibly
successful local companies.  The question is, how long is the story? I
wish I had been to Arkansas and gone to see Sam Walton of Walmart. Ten
years after Walmart went public, it was a 25-year-old company. It was
up tenfold. I said, “Ooh, I missed that one.” Then it went up 50-fold.
Sherwin Williams earnings are up 20-fold since I managed the stock in
Magellan. The professional painter in our little town goes to Sherwin
Williams, not to Home Depot. Why didn’t I look at Sherwin Williams?
Why didn’t I spend an hour on that? So staggering, dumb, or just lazy.
I thought it was the last inning of the ballgame, without doing the
research.  You can’t make these conclusions without some basis. Really
bad American fast food has done brilliantly overseas. There are 1,400
McDonald’s in France. You want to be in [the stock] in the second
inning of the ballgame, and out in the seventh. That could be 30
years. Like the people who were really wrong on McDonald’s. They
thought they were near the end, and they forgot about the other seven
billion people in the world.

wrap之后,阅读起来就很方便了!同时,原来lynch.txt中的换行符被忽略掉了。

textwrap.wrap还可以指定一个width:

>>> for i in textwrap.wrap(tt,48): print(i)
...
My best stocks have been ones where I didn’t
have to worry about the big picture. A company
with a better mousetrap, a growth company in a
non-growth industry. Stop & Shop and Dunkin’
Donuts are two incredibly successful local
companies.  The question is, how long is the
story? I wish I had been to Arkansas and gone to
see Sam Walton of Walmart. Ten years after
Walmart went public, it was a 25-year-old
company. It was up tenfold. I said, “Ooh, I
missed that one.” Then it went up 50-fold.
Sherwin Williams earnings are up 20-fold since I
managed the stock in Magellan. The professional
painter in our little town goes to Sherwin
Williams, not to Home Depot. Why didn’t I look
at Sherwin Williams? Why didn’t I spend an hour
on that? So staggering, dumb, or just lazy. I
thought it was the last inning of the ballgame,
without doing the research.  You can’t make
these conclusions without some basis. Really bad
American fast food has done brilliantly
overseas. There are 1,400 McDonald’s in France.
You want to be in [the stock] in the second
inning of the ballgame, and out in the seventh.
That could be 30 years. Like the people who were
really wrong on McDonald’s. They thought they
were near the end, and they forgot about the
other seven billion people in the world.

textwrap.fill函数

textwrap.wrap函数返回的是list,textwrap.fill返回的就是一个分隔好的长字符串,可以直接打印出来。

>>> print(textwrap.fill(tt))
My best stocks have been ones where I didn’t have to worry about the
big picture. A company with a better mousetrap, a growth company in a
non-growth industry. Stop & Shop and Dunkin’ Donuts are two incredibly
successful local companies.  The question is, how long is the story? I
wish I had been to Arkansas and gone to see Sam Walton of Walmart. Ten
years after Walmart went public, it was a 25-year-old company. It was
up tenfold. I said, “Ooh, I missed that one.” Then it went up 50-fold.
Sherwin Williams earnings are up 20-fold since I managed the stock in
Magellan. The professional painter in our little town goes to Sherwin
Williams, not to Home Depot. Why didn’t I look at Sherwin Williams?
Why didn’t I spend an hour on that? So staggering, dumb, or just lazy.
I thought it was the last inning of the ballgame, without doing the
research.  You can’t make these conclusions without some basis. Really
bad American fast food has done brilliantly overseas. There are 1,400
McDonald’s in France. You want to be in [the stock] in the second
inning of the ballgame, and out in the seventh. That could be 30
years. Like the people who were really wrong on McDonald’s. They
thought they were near the end, and they forgot about the other seven
billion people in the world.
>>>
>>> print(textwrap.fill(tt,48))
My best stocks have been ones where I didn’t
have to worry about the big picture. A company
with a better mousetrap, a growth company in a
non-growth industry. Stop & Shop and Dunkin’
Donuts are two incredibly successful local
companies.  The question is, how long is the
story? I wish I had been to Arkansas and gone to
see Sam Walton of Walmart. Ten years after
Walmart went public, it was a 25-year-old
company. It was up tenfold. I said, “Ooh, I
missed that one.” Then it went up 50-fold.
Sherwin Williams earnings are up 20-fold since I
managed the stock in Magellan. The professional
painter in our little town goes to Sherwin
Williams, not to Home Depot. Why didn’t I look
at Sherwin Williams? Why didn’t I spend an hour
on that? So staggering, dumb, or just lazy. I
thought it was the last inning of the ballgame,
without doing the research.  You can’t make
these conclusions without some basis. Really bad
American fast food has done brilliantly
overseas. There are 1,400 McDonald’s in France.
You want to be in [the stock] in the second
inning of the ballgame, and out in the seventh.
That could be 30 years. Like the people who were
really wrong on McDonald’s. They thought they
were near the end, and they forgot about the
other seven billion people in the world.

此函的功能,还可以这样来表达:

>>> '\n'.join(textwrap.wrap(tt))

textwrap.shorten函数

First the whitespace in text is collapsed (all whitespace is replaced by single spaces). If the result fits in the width, it is returned. Otherwise, enough words are dropped from the end so that the remaining words plus the placeholder fit within width。(这段文字解释了什么是whitespace collapse,就是多个空格合并成1个,tab也被认为是whitespace)

>>> textwrap.shorten('www    pynote    net', width=16)
'www pynote net'
>>> textwrap.shorten('www    pynote    net', width=12)
'www [...]'
>>> textwrap.shorten('www    pynote    net', width=12, placeholder='...')
'www...'
>>> textwrap.shorten('www    pynote    net', width=12, placeholder=' ...')
'www ...'

这个placeholder还nice。

textwrap.dedent函数

将text每一行前面的whitespace去掉。这个函数常常用来在代码中处理大段出现的需要打印的说明文字(比如使用argparse模块做命令行程序时),由于写代码需要某种对齐,而打印的时候,正好使用此函数去掉前面因为对齐而多余的whitespace:

>>> lump_string = '''\
...         www.pynote.net is my second blog site,
...         www.maixj.net is the first one.'''
>>> textwrap.dedent(lump_string)
'www.pynote.net is my second blog site,\nwww.maixj.net is the first one.'
>>> print(textwrap.dedent(lump_string))
www.pynote.net is my second blog site,
www.maixj.net is the first one.

textwrap.indent函数

indent就是给每一行文本增加prefix,prefix可以是几个空格,也可以是其它指定的字符。默认情况下,textwrap.indent函数只对非空白行家prefix:

>>> duo_line = 'hi, there\nthis is www.pynote.net.\n\nby: maixj.net'
>>> print(duo_line)
hi, there
this is www.pynote.net.

by: maixj.net
>>> print(textwrap.indent(duo_line,'    '))
    hi, there
    this is www.pynote.net.

    by: maixj.net
>>> print(textwrap.indent(duo_line,'+ '))
+ hi, there
+ this is www.pynote.net.

+ by: maixj.net

如果希望对包括空白行的所有行增加prefix,可以这样:

>>> print(textwrap.indent(duo_line,'** ', lambda line:True))
** hi, there
** this is www.pynote.net.
**
** by: maixj.net

以上所有的便捷的函数功能,都可以通过textwrap.TextWrapper对象来提供,以后有机会再介绍吧。

-- EOF --

本文链接:https://www.pynote.net/archives/1703

留言区

电子邮件地址不会被公开。 必填项已用*标注


前一篇:
后一篇:

More


©Copyright 麦新杰 Since 2019 Python笔记

go to top