当文本不适合屏幕并返回行时,如何更改 python 行为?



我的问题有点难以解释,我做了一个程序,将列表对齐到最长的类名,我们在这张图中看到的是我的程序在启动它时显示给我的:S37和38在同一个地方,而不是类名,因为python一旦到达屏幕边界就会回到行。

现在,这就是我想要的。显然,在这种情况下,我用空格来打印它,以显示它应该是什么样子,但程序必须适应所有屏幕尺寸,所以这种方法根本不可行。有没有一种方法可以让所有的文本都对齐,不管屏幕大小如何?

我使用的示例列表(list[x][0]是类名,list[x][1]是类):

[['2', ['bob', 'patrick', 'mike', 'jhamed', 'brook', 'gianni', 'espoir', 'igor', 'estère', 'charlotte', 'marie-charlotte', 'charlotte-marie', 'pénellope', 'cunégonde', 'félicie', "zo'", 'esmeralda', 'canada']], ['longclass', ['S1', 'S2', 'S3', 'S4', 'S5', 'S6', 'S7', 'S8', 'S9', 'S10', 'S11', 'S12', 'S13', 'S14', 'S15', 'S16', 'S17', 'S18', 'S19', 'S20', 'S21', 'S22', 'S23', 'S24', 'S25', 'S26', 'S27', 'S28', 'S29', 'S30', 'S31', 'S32', 'S33', 'S34', 'S35', 'S36', 'S37', 'S38']], ['test2', ['S1', 'S2', 'S3', 'S4', 'S6']], ['extremely long class name', ['s1', 's2', 's3', 's4']]]

我用来添加空格对齐的代码(在找到最大类名的程序之后使用):

for j in range(len(List)) :
var = List[j]
for i in range(length - len(List[j])) : #"length" is the number of letters in the biggest class name, with "extremely long class name" : 25
var = var + " "
print(j,length,length - len(List[j]),var,exemple)

解决方案:

import shutil
maxWidth = shutil.get_terminal_size()[0]
buf = ''
List = [['2', ['bob', 'patrick', 'mike', 'jhamed', 'brook', 'gianni', 'espoir', 'igor', 'estère', 'charlotte', 
'marie-charlotte', 'charlotte-marie', 'pénellope', 'cunégonde', 'félicie', "zo'", 'esmeralda', 'canada']],
['longclass', ['S1', 'S2', 'S3', 'S4', 'S5', 'S6', 'S7', 'S8', 'S9', 'S10', 'S11', 'S12', 'S13', 'S14',
'S15', 'S16', 'S17', 'S18', 'S19', 'S20', 'S21', 'S22', 'S23', 'S24', 'S25', 'S26', 'S27', 'S28', 'S29', 'S30', 'S31', 'S32', 'S33', 'S34', 'S35', 'S36', 'S37', 'S38']],
['test2', ['S1', 'S2', 'S3', 'S4', 'S6']], 
['extremely long class name', ['s1', 's2', 's3', 's4']]
]
classnameMaxLen = len(max( List, key=lambda L: len(L[0]))[0]) # return longest classname length
def flushBuf():
global buf
print( buf)
buf = ''
def addClassnameToBuf( txt):
global buf
if len(buf): # Something from previous class?
flushBuf()
buf += txt + ': ' + ' ' * (classnameMaxLen - len(txt))

def addNameToBuf( txt:str):
global buf

if len(buf) + len(txt) >= maxWidth:
flushBuf()
if len(buf) == 0: # No classname means it's a continuation line.
buf += (' ' * (classnameMaxLen  + len( ': ')))

buf += txt
# Print column titles (optional)
addClassnameToBuf( "Class")
addNameToBuf( "Names")
for klass in List:
addClassnameToBuf( klass[0])
for i,name in enumerate( klass[1]):
addNameToBuf( name + (', ' if i+1 != len(klass[1]) else ''))
flushBuf() # final flush

结果是:

Class:                     Names
2:                         bob, patrick, mike, jhamed, 
brook, gianni, espoir, igor, 
estère, charlotte, 
marie-charlotte, 
charlotte-marie, pénellope, 
cunégonde, félicie, zo', 
esmeralda, canada
longclass:                 S1, S2, S3, S4, S5, S6, S7, 
S8, S9, S10, S11, S12, S13, 
S14, S15, S16, S17, S18, S19, 
S20, S21, S22, S23, S24, S25, 
S26, S27, S28, S29, S30, S31, 
S32, S33, S34, S35, S36, S37, 
S38
test2:                     S1, S2, S3, S4, S6
extremely long class name: s1, s2, s3, s4
一个更优雅的解决方案可能是使用面向对象的设计。
如果你需要帮助,尽管说。注意:在"zo"附近有一个多余的撇号。名字。

我用shutil玩了一会儿,这是我设法制作的版本。显然,@user3435121的脚本也可以工作,甚至更好一点,因为它没有削减一半的学生的名字。谢谢大家的帮助。

List = [['2', ['bob', 'patrick', 'mike', 'jhamed', 'brook', 'gianni', 'espoir', 'igor', 'estère', 'charlotte', 'marie-charlotte', 'charlotte-marie', 'pénellope', 'cunégonde', 'félicie', "zo'", 'esmeralda', 'canada']], ['longclass', ['S1', 'S2', 'S3', 'S4', 'S5', 'S6', 'S7', 'S8', 'S9', 'S10', 'S11', 'S12', 'S13', 'S14', 'S15', 'S16', 'S17', 'S18', 'S19', 'S20', 'S21', 'S22', 'S23', 'S24', 'S25', 'S26', 'S27', 'S28', 'S29', 'S30', 'S31', 'S32', 'S33', 'S34', 'S35', 'S36', 'S37', 'S38']], ['test2', ['S1', 'S2', 'S3', 'S4', 'S6']], ['extremely long class name', ['s1', 's2', 's3', 's4']]]
import shutil
def findLongestClassName(List):
longest = 0
for i in range(len(List)):
if len(List[i][0]) > longest :
longest = len(List[i][0])
return longest
def printClasses(List) :
matrice = " : "
longest = findLongestClassName(List)+len(matrice)
screenx,screeny = shutil.get_terminal_size()
lengthOfSingleLine = screenx-longest
if longest >= screenx :
print("too narrow window")
return None
for i in range(len(List)) :
cleanClass = str(List[i][1]).replace("[", "").replace("]", "").replace("'", "")
cleanClassList = list(cleanClass) #imossible to edit a str directly
availableSpace = lengthOfSingleLine
while len(cleanClassList) > availableSpace :
if cleanClassList[availableSpace] == " " : #not necessary, it's just to make indent cleaner
del cleanClassList[availableSpace]
if cleanClassList[availableSpace] == "," and cleanClassList[availableSpace+1] == " " :
del cleanClassList[availableSpace]
del cleanClassList[availableSpace]
cleanClassList[availableSpace] = " "*longest+cleanClassList[availableSpace]
availableSpace += lengthOfSingleLine
cleanClass = ""
for j in cleanClassList:
cleanClass += j
print(List[i][0]+" "*(longest-len(matrice)-len(List[i][0]))+matrice+cleanClass)

while True :
print(" - - - - - - - - - start - - - - - - - - - ")
printClasses(List)
input()

结果:result

最新更新