繁体   English   中英

sys.stdout.write \\ r回车,如何覆盖所有字符?

[英]sys.stdout.write \r carriage, how to overwrite all characters?

我正在使用itertools.cycle,正在使用一个简单的列表作为输入。 然后,我编写一个while循环,当我遍历它们时,我想基本上用每种颜色覆盖我的输出。 sys.stdout.write('\\r' + colors)行不会覆盖所有字符,仅覆盖下一个颜色的字符串的长度。 最后,每次迭代之间有0.5秒的延迟。

import itertools
import time
colors = ['green', 'yellow', 'red']
traffic_light = itertools.cycle(colors)
while True:
    sys.stdout.write('\r' + next(traffic_light))
    sys.stdout.flush()
    time.sleep(.5)

当我在循环中到达“黄色”时,当打印较短的“绿色”和“红色”字符串时,我留下“ w”或“ low”。 我的输出看起来像这样(在打印“ yellow”时的第一个循环之后)。

redlow
greenw
yellow

我可以用'\\r'完全覆盖输出吗?

回车符'\\r'会将光标发送到该行的开头,在此处可以覆盖现有文本。 您可以将其与序列CSI K结合使用,该序列将从当前光标擦除到行尾。

只需将\\r替换为\\r\\x1b[K 请参阅ANSI转义代码

import itertools
import sys
import time
colors = ['green', 'yellow', 'red']
traffic_light = itertools.cycle(colors)
while True:
    sys.stdout.write('\r\x1b[K' + next(traffic_light))
    sys.stdout.flush()
    time.sleep(.5)

试用以下其他转义序列:

# Add color
colors = ['\x1b[32mgreen', '\x1b[33myellow', '\x1b[31mred']

请注意此技术的局限性...如果终端足够短,以至于您的文字不能换行,则每次打印时程序都会向前移动一行。 如果您需要更强大的功能, curses可为您提供更多功能,但在Windows上无法立即使用。

您可以计算颜色字符串的最大宽度,然后使用str.ljust在输出中填充足够的空间以填充到最大宽度:

import itertools
import time
import sys
colors = ['green', 'yellow', 'red']
traffic_light = itertools.cycle(colors)
max_width = max(map(len, colors))
while True:
    sys.stdout.write('\r' + next(traffic_light).ljust(max_width))
    sys.stdout.flush()
    time.sleep(.5)

创建一个格式字符串,该字符串左对齐至最大宽度。

import itertools
import time

colors = ['green', 'yellow', 'red']
fmt = f'\r{{:<{max(map(len, colors))}}}' # fmt = '{:<7}'

for color in itertools.cycle(colors):
    print(fmt.format(color), end='') # if needed add: flush=True
    time.sleep(.5)

在3.6之前的版本中使用fmt = '\\r{{:<{}}}'.format(max(map(len, colors)))

或者,使用.ljust()字符串方法:

import itertools
import time

colors = ['green', 'yellow', 'red']
width = max(map(len, colors))

for color in itertools.cycle(colors):
    print('\r' + color.ljust(width), end='') # if needed add: flush=True
    time.sleep(.5)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM