【发布时间】:2017-10-20 07:36:14
【问题描述】:
以前有人问过这个问题(例如a、b 等),但我无法使其在 IPython 控制台中工作。 我只是想在同一行上一遍又一遍地打印一些文本,而不是连续文本,而是替换前一个。 这是我的代码:
import time
try:
while True:
s = "I want this always on the same line. "
print(s, end="\r", flush=True)
time.sleep(0.5)
except KeyboardInterrupt:
pass
这是我在 IPython 控制台中得到的:
我在 PC 和 Spyder 上使用 Python 3 上的 Anaconda 发行版。代码在 Anaconda 终端中工作,但我还需要打印一些图像,所以我需要 IPython。我还尝试使用end="" 或在print() 语句后添加逗号(即使这是针对Python 2),但无济于事。
【问题讨论】:
-
print("hello", end="\r")在我的控制台(终端)上运行良好。 -
我得到“hellohellohellohellohellohellohellohellohellohellohello”...
-
尝试在终端中运行它,
python3 run.py@Zep -
啊!回车确实在终端有效!但我需要使用 IPython,因为我也想输出一些图像......我将编辑问题
标签: python printing console ipython