python 输出中的居中多行文本
center multi-line text in python output
好吧,这似乎是一个非常基本的问题,但我在任何地方都找不到可行的答案,所以就这样吧。
我有一些文字:
text = '''
Come and see the violence inherent in the system. Help! Help! I'm being
repressed! Listen, strange women lyin' in ponds distributin' swords is no
basis for a system of government. Supreme executive power derives from a
mandate from the masses, not from some farcical aquatic ceremony. The Lady
of the Lake, her arm clad in the purest shimmering samite held aloft
Excalibur from the bosom of the water, signifying by divine providence that
I, Arthur, was to carry Excalibur. THAT is why I am your king.'''
它不包含任何换行符或其他格式。
我想包装我的文本,以便当我 运行 我的代码时它在我的 ipython 输出 window 中正确显示。我也希望它居中,并且比完整 window 宽度(80 个字符)
短一点
如果我有一个短文本字符串(比行长短),我可以简单地计算字符串的长度并用空格填充它以使其居中,或者使用 text.center()
属性 才能正确显示。
如果我有一个只想换行的文本字符串,我可以使用:
from textwrap import fill
print(fill(text, width=50))
并将宽度设置为任意值
所以我本来以为我可以简单地:
from textwrap import fill
wrapped_text = (fill(text, width=50))
print(wrapped_text.center(80))
但它不起作用。一切仍然合理。
我敢肯定我不是唯一尝试这样做的人。有人可以帮我吗?
wrapped_text
是一个字符串列表,因此循环遍历字符串并将它们居中。
import textwrap
text = "Come and see the violence inherent in the system. Help! Help! I'm being repressed! Listen, strange women lyin' in ponds distributin' swords is no basis for a system of government. Supreme executive power derives from a mandate from the masses, not from some farcical aquatic ceremony. The Lady of the Lake, her arm clad in the purest shimmering samite held aloft Excalibur from the bosom of the water, signifying by divine providence that I, Arthur, was to carry Excalibur. THAT is why I am your king."
wrapped_text = textwrap.wrap(text)
for line in wrapped_text:
print(line.center(80))
问题是 center
需要单行字符串,fill
returns 需要多行字符串。
答案是 center
每行,然后再加入它们。
如果您查看 fill
的文档,它是 shorthand:
"\n".join(wrap(text, ...))
因此,您可以跳过 shorthand 并直接使用 wrap
。例如,您可以编写自己的函数来完全满足您的需求:
def center_wrap(text, cwidth=80, **kw):
lines = textwrap.wrap(text, **kw)
return "\n".join(line.center(cwidth) for line in lines)
print(center_wrap(text, cwidth=80, width=50))
虽然如果您只在一个地方执行此操作,但要立即将其打印出来,甚至不打扰 join
可能更简单:
for line in textwrap.wrap(text, width=50):
print(line.center(80))
好吧,这似乎是一个非常基本的问题,但我在任何地方都找不到可行的答案,所以就这样吧。
我有一些文字:
text = '''
Come and see the violence inherent in the system. Help! Help! I'm being
repressed! Listen, strange women lyin' in ponds distributin' swords is no
basis for a system of government. Supreme executive power derives from a
mandate from the masses, not from some farcical aquatic ceremony. The Lady
of the Lake, her arm clad in the purest shimmering samite held aloft
Excalibur from the bosom of the water, signifying by divine providence that
I, Arthur, was to carry Excalibur. THAT is why I am your king.'''
它不包含任何换行符或其他格式。 我想包装我的文本,以便当我 运行 我的代码时它在我的 ipython 输出 window 中正确显示。我也希望它居中,并且比完整 window 宽度(80 个字符)
短一点如果我有一个短文本字符串(比行长短),我可以简单地计算字符串的长度并用空格填充它以使其居中,或者使用 text.center()
属性 才能正确显示。
如果我有一个只想换行的文本字符串,我可以使用:
from textwrap import fill
print(fill(text, width=50))
并将宽度设置为任意值
所以我本来以为我可以简单地:
from textwrap import fill
wrapped_text = (fill(text, width=50))
print(wrapped_text.center(80))
但它不起作用。一切仍然合理。
我敢肯定我不是唯一尝试这样做的人。有人可以帮我吗?
wrapped_text
是一个字符串列表,因此循环遍历字符串并将它们居中。
import textwrap
text = "Come and see the violence inherent in the system. Help! Help! I'm being repressed! Listen, strange women lyin' in ponds distributin' swords is no basis for a system of government. Supreme executive power derives from a mandate from the masses, not from some farcical aquatic ceremony. The Lady of the Lake, her arm clad in the purest shimmering samite held aloft Excalibur from the bosom of the water, signifying by divine providence that I, Arthur, was to carry Excalibur. THAT is why I am your king."
wrapped_text = textwrap.wrap(text)
for line in wrapped_text:
print(line.center(80))
问题是 center
需要单行字符串,fill
returns 需要多行字符串。
答案是 center
每行,然后再加入它们。
如果您查看 fill
的文档,它是 shorthand:
"\n".join(wrap(text, ...))
因此,您可以跳过 shorthand 并直接使用 wrap
。例如,您可以编写自己的函数来完全满足您的需求:
def center_wrap(text, cwidth=80, **kw):
lines = textwrap.wrap(text, **kw)
return "\n".join(line.center(cwidth) for line in lines)
print(center_wrap(text, cwidth=80, width=50))
虽然如果您只在一个地方执行此操作,但要立即将其打印出来,甚至不打扰 join
可能更简单:
for line in textwrap.wrap(text, width=50):
print(line.center(80))