【问题标题】:python: replace a printed line's text with another linepython:用另一行替换打印行的文本
【发布时间】:2021-05-27 05:09:52
【问题描述】:

我想打印一行并将该行替换为另一个文本。 示例代码:

import os
import time
lst = ["|","/","-","\\"]
num = 0
for i in range(500):
    os.system("cls") #i don't want to clear the whole terminal window.
    print(lst[num])
    if num == 3:
        num = 0
    else:
        num = num + 1
    time.sleep(0.2)

就像我们使用 pip 安装 tar.gz 文件一样 在构建包的 setup.py 时,它会打印“building setup.py ,|,-,\”而不清除控制台..

我看过很多关于这个的帖子..但他们都没有一个好的答案..

SPECS:
OS: Windows 7 SP1
Python: Python 3.8.10
Arch: x86 (32-bit)

【问题讨论】:

标签: python printing console


【解决方案1】:

您可以使用\r(回车)。你也不需要num作为索引lst的额外变量你可以使用现有的i来索引lst

import time
lst = ["|","/","-","\\"]
num = 0
for i in range(500):
    print(lst[i % 4], end="\r")     # i will be between 0 and 3 both inclusive
    time.sleep(0.2)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-02
    • 2022-06-15
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多