python中文本周围的动态边框

时间:2016-02-13 14:23:32

标签: python

我需要输入一个句子并在该句子周围创建一个动态边框。边框需要输入宽度。当句子的长度高于给定的宽度时,必须打印一个新行,并且边框必须在高度中更改。句子也必须以动态边界为中心

我已经尝试过了:

sentence = input()
width = int(input())

length_of_sentence = len(sentence)

print('+-' + '-'*(width) + '-+')

for letter in sentence:
    print('| {0:^{1}} |'.format(letter, width - 4))

print('+-' + '-'*(width) + '-+')

然后打印出一个新行的每个字母,这不是我需要的。

以下是一个很好的例子;

输入

sentence = "You are only young once, but you can stay immature indefinitely."
width = 26

输出

+----------------------------+
| You are only young once, b |
| ut you can stay immature i |
|         ndefinitely.       |
+----------------------------+

4 个答案:

答案 0 :(得分:5)

如果你想避免在中间破坏单词,你也可以使用textwrap.wrap

from textwrap import wrap

sentence = input('Sentence: ')
width = int(input('Width: '))

print('+-' + '-' * width + '-+')

for line in wrap(sentence, width):
    print('| {0:^{1}} |'.format(line, width))

print('+-' + '-'*(width) + '-+')

输出:

+----------------------------+
|  You are only young once,  |
| but you can stay immature  |
|       indefinitely.        |
+----------------------------+

答案 1 :(得分:3)

因此,您不想进行字母输入,而是希望将字符串拆分为chunks of width letters。接受接受的答案:

def chunkstring(string, length):
    return (string[0+i:length+i] for i in range(0, len(string), length))

sentence = input('Sentence: ')
width = int(input('Width: '))

print('+-' + '-' * width + '-+')

for line in chunkstring(sentence, width):
    print('| {0:^{1}} |'.format(line, width))

print('+-' + '-'*(width) + '-+')

示例运行:

Sentence: You are only young once, but you can stay immature indefinitely. 
Width: 26
+----------------------------+
| You are only young once, b |
| ut you can stay immature i |
|       ndefinitely.         |
+----------------------------+

答案 2 :(得分:2)

我会使用PrettyTable模块完成这项任务 - 它会照顾好#34;很好地"印刷:

2016-02-13 09:31:04 [scrapy] INFO: Enabled item pipelines:
[]
2016-02-13 09:31:04 [scrapy] INFO: Spider opened
2016-02-13 09:31:04 [scrapy] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)

[... bunch of Selenium messages ...]

2016-02-13 09:31:13 [scrapy] DEBUG: Crawled (200) <GET http://webutil.bridgebase.com/v2/tarchive.php?m=h&h=acbl&d=ACBL&o=acbh> (referer: None)

[... more Selenium messages ...]

2016-02-13 09:31:16 [scrapy] DEBUG: Crawled (200) <GET http://www.bridgebase.com/myhands/hands.php?tourney=4796-1455303720-> (referer: http://webutil.bridgebase.com/v2/tarchive.php?m=h&h=acbl&d=ACBL&o=acbh)
2016-02-13 09:31:17 [scrapy] INFO: Closing spider (finished)
2016-02-13 09:31:17 [scrapy] INFO: Dumping Scrapy stats:

输出:

import prettytable as pt

sentence = "You are only young once, but you can stay immature indefinitely."
width = 26


t = pt.PrettyTable()

t.field_names = ['output']
[t.add_row([sentence[i:i + width]]) for i in range(0, len(sentence), width)]

print(t)

答案 3 :(得分:0)

import math

sentence = input()
width = int(input())

length_of_sentence = len(sentence)

print('+-' + '-'*(width) + '-+')

i = 0
lines = int(math.ceil(length_of_sentence/float(width)))
for l in xrange(lines): 
    line = sentence[i:i+width]
    if len(line) < width:
        padding = (width - len(line))/2
        line = padding*' ' + line + padding*' ' 
    print('| {0} |'.format(line))
    i += width

print('+-' + '-'*(width) + '-+')