无法在VS Code笔记本中同一行打印,`\b`和`\r`无效

0 投票
2 回答
58 浏览
提问于 2025-04-14 16:16

我想在VS Code的笔记本里打印在同一行,但这不太好使。

\b也不太管用,它让我需要用两个\b组合在一起才能退格。

我安装了Jupyter Notebook扩展。而且我也在Jupyter笔记本里试过,结果和在VS Code的笔记本里一样。

这是用来重现这个程序的代码:

import time


def test_output():
        print('\b\b \b\b: OK', end='', flush=True)
        print(' -> Read: OK', end='', flush=True)
        print(' -> Json: OK', end='', flush=True)
        print(' -> Analysing...', end='', flush=True)


for i in range(15):
    print(f'\r{" " * 20}\r', end="", flush=True)
    time.sleep(1.5)
    print(f"\rtest({i:02d}/15): Opening", end="", flush=True)
    time.sleep(1.5)
    test_output()

这是输出结果:

test(14/15): OpeningK -> Read: OK -> Json: OK -> Analysing: OK -> Read: OK -> Json: OK -> Analysing: OK -> Read: OK -> Json: OK -> Analysing: OK -> Read: OK -> Json: OK -> Analysing: OK -> Read: OK -> Json: OK -> Analysing: OK -> Read: OK -> Json: OK -> Analysing: OK -> Read: OK -> Json: OK -> Analysing: OK -> Read: OK -> Json: OK -> Analysing: OK -> Read: OK -> Json: OK -> Analysing: OK -> Read: OK -> Json: OK -> Analysing: OK -> Read: OK -> Json: OK -> Analysing: OK -> Read: OK -> Json: OK -> Analysing: OK -> Read: OK -> Json: OK -> Analysing: OK -> Read: OK -> Json: OK -> Analysing: OK -> Read: OK -> Json: OK -> Analysing...

这是\b不工作的代码和输出:

import time


def test_output():
        print('\b\b\b: OK', end='', flush=True)
        print(' -> Read: OK', end='', flush=True)
        print(' -> Json: OK', end='', flush=True)
        print(' -> Analysing...', end='', flush=True)


for i in range(15):
    print(f'\r{" " * 20}\r', end="", flush=True)
    time.sleep(1.5)
    print(f"\rtest({i:02d}/15): Opening", end="", flush=True)
    time.sleep(1.5)
    test_output()
test(14/15): Opening OK -> Read: OK -> Json: OK -> Analysing..: OK -> Read: OK -> Json: OK -> Analysing..: OK -> Read: OK -> Json: OK -> Analysing..: OK -> Read: OK -> Json: OK -> Analysing..: OK -> Read: OK -> Json: OK -> Analysing..: OK -> Read: OK -> Json: OK -> Analysing..: OK -> Read: OK -> Json: OK -> Analysing..: OK -> Read: OK -> Json: OK -> Analysing..: OK -> Read: OK -> Json: OK -> Analysing..: OK -> Read: OK -> Json: OK -> Analysing..: OK -> Read: OK -> Json: OK -> Analysing..: OK -> Read: OK -> Json: OK -> Analysing..: OK -> Read: OK -> Json: OK -> Analysing..: OK -> Read: OK -> Json: OK -> Analysing..: OK -> Read: OK -> Json: OK -> Analysing...

这些问题只在笔记本中出现,无论是在VS Code还是Jupyter笔记本里。但如果我在终端运行,就能正常工作。

2 个回答

0

正如这里提到的,Jupyter Notebook只是“模拟”了一个终端;因为\b是根据光标的位置来工作的,而Notebook并没有模拟光标的位置,所以\b是不支持的。

0

如果你的演示能够正常工作,具体应该发生什么呢?你没有说明。

基于Jupyter的选项

因为\b在Jupyter中无法使用,正如其他人所说的那样:
我觉得这个方法差不多可以(它是基于这里的内容):

import time

def test_output():
        print(f'{curr_prog}: Open: OK -> Read: OK  -> Json: OK  -> Analysing...', end=' ', flush=True)


for i in range(15):
    curr_prog=f"\rtest({i:02d}/15)"
    print(f"{curr_prog}: Opening                                            ", end="\r", flush=True)
    time.sleep(1.5)
    test_output()
    if i < 14:
        time.sleep(1.5)

在Jupyter中丰富的演示,改编自file_progress.py

from time import sleep
from urllib.request import urlopen

from rich.progress import wrap_file

# Read a URL with urlopen
response = urlopen("https://www.textualize.io")
# Get the size from the headers
size = int(response.headers["Content-Length"])

# Wrap the response so that it update progress

with wrap_file(response, size) as file:
    for line in file:
        #print(line.decode("utf-8"), end="")
        sleep(0.1)

撰写回答