1

我不知道这是否可以做到,但我确定我在一些脚本中看到了这一点,这就是我问的原因。

我需要打印一个像这样的字符串:

++++++++++++++++++++++++++++++     +++++++++++++++
+     A 1     ++     A 2     +     +     A n     +
+-------------++-------------+     +-------------+
+     B 1     ++     B 2     + ... +     B n     +
+-------------++-------------+     +-------------+
+     C 1     ++     C 2     +     +     C n     +
++++++++++++++++++++++++++++++     +++++++++++++++

其中 n 是列数,它取决于用户的输入。A 行是固定的,而 B 和 C 必须在程序运行时更改。

所以,首先我需要一种打印这种字符串的方法,知道 A 和 B 是长度为 8 的字符串,但 C 从 8 变为 1 个字符。

我查看了各种“格式化程序”解决方案,ppretty但它们似乎离我需要的太远了(而且我没有找到很多例子!)。(我刚刚尝试过ppretty,因为其他解决方案在我获取数据时需要数据源之类的东西,class.getData()因为我来自 Java!)

现在,在打印这个字符串之后,我希望它随着 B 和 C 的变化而更新,而不是再次打印它以避免大量打印的东西,并使所有内容更整洁,更容易阅读。

有没有办法做到这一点?

编辑:

这是我尝试过的(没有成功)

def printCrackingState(threads):
info_string = '''
++++++++++++++++++++++++++++++++++
+ Starting password = s.%08d +
+--------------------------------+    
+ Current pin       = s.%08d +
++++++++++++++++++++++++++++++++++
+ Missing pins      =   %08d +
++++++++++++++++++++++++++++++++++
              '''
while 1:
    for t in threads:
        printed_string = info_string % (t.starting_pin, t.testing_pin, t.getMissingPinsCount())
        sys.stdout.write(printed_string)
    time.sleep(3500)

这是结果:

++++++++++++++++++++++++++++++++++
+ Starting password = s.00000000 +
+--------------------------------+    
+ Current pin       =   00000523 +
++++++++++++++++++++++++++++++++++
+ Missing pins      =   01249477 +
++++++++++++++++++++++++++++++++++

++++++++++++++++++++++++++++++++++
+ Starting password = s.01250000 +
+--------------------------------+    
+ Current pin       =   01250491 +
++++++++++++++++++++++++++++++++++
+ Missing pins      =   01249509 +
++++++++++++++++++++++++++++++++++

++++++++++++++++++++++++++++++++++
+ Starting password = s.02500000 +
+--------------------------------+    
+ Current pin       =   02500465 +
++++++++++++++++++++++++++++++++++
+ Missing pins      =   01249535 +
++++++++++++++++++++++++++++++++++

++++++++++++++++++++++++++++++++++
+ Starting password = s.03750000 +
+--------------------------------+    
+ Current pin       =   03750564 +
++++++++++++++++++++++++++++++++++
+ Missing pins      =   01249436 +
++++++++++++++++++++++++++++++++++

++++++++++++++++++++++++++++++++++
+ Starting password = s.05000000 +
+--------------------------------+    
+ Current pin       =   05000592 +
++++++++++++++++++++++++++++++++++
+ Missing pins      =   01249408 +
++++++++++++++++++++++++++++++++++

++++++++++++++++++++++++++++++++++
+ Starting password = s.06250000 +
+--------------------------------+    
+ Current pin       =   06250579 +
++++++++++++++++++++++++++++++++++
+ Missing pins      =   01249421 +
++++++++++++++++++++++++++++++++++

++++++++++++++++++++++++++++++++++
+ Starting password = s.07500000 +
+--------------------------------+    
+ Current pin       =   07500577 +
++++++++++++++++++++++++++++++++++
+ Missing pins      =   01249423 +
++++++++++++++++++++++++++++++++++

++++++++++++++++++++++++++++++++++
+ Starting password = s.08750000 +
+--------------------------------+    
+ Current pin       =   08750555 +
++++++++++++++++++++++++++++++++++
+ Missing pins      =   01249445 +
++++++++++++++++++++++++++++++++++

我曾经sys.stdout.write()将它们放在同一条线上,但它不起作用!

编辑2:

正如回复中所建议的,我的第二次尝试是诅咒。

我可以在同一行写东西,但它们不会更新。

这是我的代码:

import curses
import time
import threading

class CursesPrinter(threading.Thread):
    windows = []
    screen = None 
    processes = []   

    info_string = '''
    +++++++++++++++++++++++++
    + Starting = s.%08d +
    +-----------------------+    
    + Current  = s.%08d +
    +-----------------------+
    + Missing  =   %08d +
    +++++++++++++++++++++++++
                  '''            

    def _makeWindows(self, numWindows):
        x = 0
        y = 0
        height = 15
        width = 30        
        for i in range(numWindows):
            win = curses.newwin(height, width, x, y)
            #win.border(1)
            y+=width
            if y>self.screen.getmaxyx():
                #This should make a new line if we reach the end of the terminal
                y = 0
                x+=height
            self.windows.append(win) 

    def run(self):
        while 1:
            for i in range(len(self.processes)-1):
                print_string = self.info_string % (self.processes[i].starting_pin, self.processes[i].testing_pin, self.processes[i].getMissingPinsCount())                
                self.windows[i].addstr(0,0, print_string)
                self.windows[i].refresh()
        #time.sleep(60)                            


    def __init__(self, threads, processes):
        super(CursesPrinter, self).__init__()
        self.screen = curses.initscr()
        curses.curs_set(0)
        self.processes = processes
        self._makeWindows(threads)       
        #curses.endwin()  
4

2 回答 2

1

依靠终端处理VT100 转义序列的一种快速而简单的方法是清除屏幕,然后在打印输出之前将光标移动到起始位置。

快速演示:

import sys
import time
import random

ESC = "\x1b"

def home():
    sys.stdout.write(ESC + '[0;0H')
    sys.stdout.flush()

def cls():
    sys.stdout.write(ESC + '[2J')
    sys.stdout.flush()

def print_status():
    w = 8
    print('+' + '-'*w + '+')
    for row in range(3):
        fmt = '|%%%dd |' % (w - 1)
        print(fmt % random.randint(0, 1000))
        print('+' + '-'*w + '+')
    sys.stdout.flush()

if __name__ == "__main__":
    cls()
    for k in range(16, 0, -1):
        home()  # or use cls()
        print_status()
        time.sleep(0.5)

您可以跟踪打印的行数,而不是使用“home”序列,并在打印下一个更新之前打印那么多“cursor up”转义序列。

如果您想变得更高级,请查看 python curses 模块blessingsurwid

我认为这些都不能在常规的 Windows 终端中工作。这就是为什么我询问您所针对的操作系统的原因。

于 2013-03-29T12:32:13.187 回答
0

您可以打印出许多换行符,使其看起来像屏幕已被清除。它也易于实现并且有助于调试。

代码将类似于:

print "\n"*100

于 2013-12-07T03:19:57.593 回答