Python-创建具有动态大小的文本边框
我正在创建一个命令行脚本,我希望有一个框Python-创建具有动态大小的文本边框,python,command-line,border,Python,Command Line,Border,我正在创建一个命令行脚本,我希望有一个框 +--------+ | | | | | | +--------+ 。。。这将永远符合它的内容。我知道如何做顶部和底部,但它让ljust和rjust正常工作。每行可能有一个字符串替换,或5个,这些字符串的len可以是0到80之间的任何值 我一直在做这样的事情: 打印“|%s |”%(my_string.ljust(80 len(my_string))) 但该死的是,这太乱了。。。这只是一个硬编码的替换。我不
+--------+
| |
| |
| |
+--------+
。。。这将永远符合它的内容。我知道如何做顶部和底部,但它让ljust和rjust正常工作。每行可能有一个字符串替换,或5个,这些字符串的len可以是0到80之间的任何值
我一直在做这样的事情:
打印“|%s |”%(my_string.ljust(80 len(my_string)))
但该死的是,这太乱了。。。这只是一个硬编码的替换。我不知道如何使它变得动态,比如说第一行有2个sub,第二行有3个sub,第三行有1个sub(所有这些都是列格式)
因此,作为一个基本示例,我需要:
+--------+
| 1 |
| 1 2 3 |
| 1 2 |
+--------+
您可以使用适用于Linux和Mac的python标准库中的模块。您也可以尝试Mac、Linux和Windows的library。
你也可以。
但对于简单对话框,您可以使用下一个代码:
def my_text_frame(string_lst, width=20):
g_line = "+{0}+".format("-"*(width-2))
print g_line
for line in string_lst:
print "| {0:<{1}} |".format(line, width-4)
print g_line
my_text_frame("""Some text
123456 789
123""".splitlines())
对于字符串格式的宽度和精度字段,可以使用
“*”
,如中所述。以下是一个示例:
text = """\
This is
a test of the
column formatting
system.""".splitlines()
maxlen = max(len(s) for s in text)
colwidth = maxlen + 2
print '+' + '-'*colwidth + '+'
for s in text:
print '| %-*.*s |' % (maxlen, maxlen, s)
print '+' + '-'*colwidth + '+'
印刷品:
+-------------------+
| This is |
| a test of the |
| column formatting |
| system. |
+-------------------+
我是这样做的:
def bordered(text):
lines = text.splitlines()
width = max(len(s) for s in lines)
res = ['┌' + '─' * width + '┐']
for s in lines:
res.append('│' + (s + ' ' * width)[:width] + '│')
res.append('└' + '─' * width + '┘')
return '\n'.join(res)
因此,首先将所有对象格式化为
text
warable,然后将其传递给bordered()
函数。我迟到了,但我的版本如下:
def breakLine(text, wrap=80):
if len(text) > wrap:
char = wrap
while char > 0 and text[char] != ' ':
char -= 1
if char:
text = [text[:char]] + breakLine(text[char + 1:], wrap)
else:
text = [text[:wrap - 1] + '-'] + breakLine(text[wrap - 1:], wrap)
return text
else:
return [cleanLine(text)]
def cleanLine(text):
if text[-1] == ' ':
text = text[:-1]
if text[0] == ' ':
text = text[1:]
return text
def boxPrint(text, wrap=0):
line_style = '-'
paragraph = text.split('\n')
if wrap>0:
index = 0
while index < len(paragraph):
paragraph[index] = cleanLine(paragraph[index])
if len(paragraph[index]) > wrap:
paragraph = paragraph[:index] + breakLine(paragraph[index]\
, wrap) + paragraph[index + 1:]
index += 1
length = (max([len(line) for line in paragraph]))
print '+' + line_style * length + '+'
for line in paragraph:
print '|' + line + ' ' * (length - len(line)) + '|'
print '+' + line_style * length + '+'
if __name__ == "__main__":
text = "Some text comes here to be printed in a box!!!"
boxPrint(text, 20)
text = "Title:\nBody lorem ipsum something body\ncheers,"
boxPrint(text, 20)
boxPrint(text)
text = "No Space:\nThisIsATextThatHasNoSpaceForWrappingWhichGetsBrokenUsingDashes"
boxPrint(text, 20)
你可以用它来做
输出:
+------------------+
| Some text |
| 123456 789 |
| 123 |
+------------------+
+-----------------------+
| some words that |
| could be dynamic but |
| are currently static |
+-----------------------+
from tabulate import tabulate
text = """
some words that
could be dynamic but
are currently static
"""
table = [[text]]
output = tabulate(table, tablefmt='grid')
print(output)
+-----------------------+
| some words that |
| could be dynamic but |
| are currently static |
+-----------------------+