这段代码的目的是获取一个字符串并以特定格式打印出来。例如,给定:带格式的打印:布尔问题
s = "Hello"
程序应该打印出来:
+---+---+---+---+---+
| H | e | l | l | o |
+---+---+---+---+---+
如果字符串的大小比控制台的柱尺寸更大,它则应该打印出字符串中这种格式:
+---+---+---+---+---+
| H | e | l | l | o |
+---+---+---+---+---+
| H | e | l | l | o |
+---+---+---+---+---+
| H | e | l | l | o |
+---+---+---+---+---+
| H | e | l | l | o |
+---+---+---+---+---+
不幸的是,第二个条件不起作用,我似乎无法弄清楚为什么。
这里是我的代码:
import os
s = "Hello"*20
(consoleRows,consoleCol)=os.popen('stty size','r').read().split()
top = outer = "+---"*len(s)+'+'+'\n'
for i in range(len(s)):
outer += "| "+s[i]+" "
outer += '|\n'
outer += top[:len(top)-1]
split = outer.split('\n')
if(len(split[0]) > consoleCol): #problem lies on this line. Even though the size of
outer = outer.split('\n') #split[0] is greater than consoleCol the if statement
beg = 0 #isn't entered.
size = consoleCol
print(outer[0][beg:size])
while(size < len(outer[0])):
print(outer[1][beg:size]);
print(outer[2][beg:size]);
beg = size
size += size
else:
print(outer)
有人能看到我的问题是什么?我打印出外部[0]和consoleCol的大小。 len(输出[0])大于consoleCol。
您可能想用'os.get_terminal_size()'替换对'os.popen()'的调用' – 2013-02-12 21:53:44