gpt4 book ai didi

python - 打印可变标题

转载 作者:行者123 更新时间:2023-12-01 05:30:31 24 4
gpt4 key购买 nike

我想在字符串长度变化时打印这些标题以变得更长/更短。我不知道该怎么做,我想我可以使用 len() 并根据它们的长度打印一些“=”,但结果并没有达到预期。

+==========================+
| Running: hba_test
| Skipping: abort/"Basic Sanity" delayedabort/"Abort Control List"
+==========================+
+==========================+
| Skipping: sdt_test
+==========================+

这些是 header ,这是当前用于生成它们的代码。

    #print header to seperate tests
l1 = len(x)
l2 = len(y)

print "+==========================+"
if disable:
print "| Skipping: %s " % x
else:
print "| Running: %s " % x
if 'disable=' in test:
print "| Skipping:%s " % y
print "+==========================+"

最佳答案

类似的事情怎么样?但它并不优雅且“硬编码”。

使用您的原始代码:

x = 'hba_test'
y = 'abort/"Basic Sanity" delayedabort/"Abort Control List"'
disable = False
test = 'disable='

#print header to seperate tests
l1 = len(x)
l2 = len(y)


if disable:
headerBreak ="+" + "="*(l1+12) + "+"
print headerBreak
print "| Skipping: %s |" % x
else:
headerBreak = "+" + "="*(max(l1,l2)+11) + "+"
print headerBreak
print "| Running: %s" % x, ' '*(l2-l1)+ '|'
if 'disable=' in test:
print "| Skipping:%s |" % y
print headerBreak

返回:

+=================================================================+
| Running: hba_test |
| Skipping:abort/"Basic Sanity" delayedabort/"Abort Control List" |
+=================================================================+

+====================+
| Skipping: hba_test |
+====================+
<小时/>

使用函数:

x = 'hba_test'
y = 'abort/"Basic Sanity" delayedabort/"Abort Control List"'
disable = False
test = 'disable='

#print header to seperate tests
def asciiBlock(lineList):
maxLength = 0
lines = []
for line in lineList:
newLine = ': '.join(line)
lines.append(newLine)
if maxLength<len(newLine):
maxLength=len(newLine)


asciiArt = '+'+'='*(maxLength+2)+'+'
newLineList = [asciiArt]
for line in lines:
newLineList.append('| '+ line + ' '*(maxLength-len(line))+' |')

newLineList.append(asciiArt)

return '\n'.join(newLineList)


if disable:
lineList = [('Skipping',x)]

else:
lineList = [('Running',x)]
if 'disable=' in test:
lineList.append(('Skipping',y))

print asciiBlock(lineList)

关于python - 打印可变标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20378361/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com