• Home
  • About
    • Xrlin photo

      Xrlin

      A blog for sharing my thoughts and knowledge

    • Learn More
    • Github
  • Posts
    • All Posts
    • All Tags
  • Projects

使用textwrap模块进行字符串的指定宽度输出。

03 Jun 2016

Reading time ~1 minute

python中的textwrap模块给我们提供了很方便以指定列宽重新排版字符串的方法,这在开发CLI程序时可以很方便根据终端大小进行排版输出。如:

import os
import textwrap

s = """
She had been shopping with her Mom in Wal-Mart. She must have been 6 years old, this beautiful brown haired, freckle-faced image of innocence. It was pouring outside. The kind of rain that gushes over the top of rain gutters, so much in a hurry to hit the Earth, it has no time to flow down the spout.
"""
width = os.get_terminal_size().columns # 获取终端列宽
print(textwrap.fill(s, width)) # 根据终端大小进行输出, textwrap.fill相当于'\n'.join(textwrap.wrap(s,wdth))

同时textwrap还提供了shorten这个很常用的函数给我们进行对大段文本的省略输出。

import textwrap
textwrap.shorten("Hello  world!", width=12) #'Hello world!'
textwrap.shorten("Hello  world!", width=11) #'Hello [...]'
textwrap.shorten("Hello world", width=10, placeholder="...") #'Hello...'

在网站制作时经常需要在首页显示文章的摘要,而省略后面的内容,这时textwrap这个函数与就很有用了。
textwrap还提供了dedent(text)和indent(text, prefix, predicate=Node)两个方法进行字符串行首缩进的去除和添加,需要注意的是使用dedent时\t和space是不想等的,在使用indent时prefix前缀字符不会添加到行末符号或者连续的空字符,要想实现在空行前也添加指定的前缀,可以提供predicate参数,如:

```python indent(text, prefix, predicate=lambda line: True)



Python Like Tweet +1