我尝试了以下代码
s='Python is an interpreted high-level programming language for general-purpose programming. Created by Guido van Rossum and first released in 1991, Python has a design philosophy that emphasizes code readability, notably using significant whitespace. It provides constructs that enable clear programming on both small and large scales'
s1='Python features a dynamic type system and automatic memory management. It supports multiple programming paradigms, including object-oriented, imperative, functional and procedural, and has a large and comprehensive standard library'
print(s,end='\r')
print(s1)我得到的输出是
Python is an interpreted high-level programming language for general-purpose programming. Created by Guido van Rossum and first released in 1991, Python has a design philosophy that emphasizes code readability, notably using significant whitespace. It provides con
Python features a dynamic type system and automatic memory management. It supports multiple programming paradigms, including object-oriented, imperative, functional and procedural, and has a large and comprehensive standard library第一个字符串s正在两行上打印。然后,\r从第2行开始进行第二个字符串打印。
我希望从第一个字符串的开头开始打印第二个字符串。可以用print完成吗?还是应该使用任何其他函数?
发布于 2018-10-29 09:35:26
这是您正在使用的终端模拟器的一个特性。是的,您可以使用打印来完成此操作,您需要为您的终端类型输出适当的终端转义序列。
例如,如果您使用像终端模拟器这样的xterm术语,则转义序列是在此描述 (该页面用于bash,但python具有非常类似的语法)。您可能最感兴趣的转义序列是:
如果您所做的事情要复杂得多,您可能也会对诅咒库感兴趣。
发布于 2018-10-29 10:22:11
这很棘手。首先,并非所有终端都具有相同的能力。例如,赖安所说的\033[s和\033[r不能在我的终端上工作。从终端数据库读取控制序列更安全:
import os
sc = os.popen('tput sc').read()
rc = os.popen('tput rc').read()
print(sc + s, end='\r')
print(rc + s1, end='\r')但是,这在Windows上不起作用。而且,它只是在视图中保存光标的坐标,而不是在滚动历史记录中;因此,如果将光标保存在最后一行,打印滚动屏幕和恢复光标的内容,则根本不会移动(因为它将返回--最后一行!)
我之前在评论中建议的解决办法是:
import os
columns = int(os.popen('tput cols').read())
need_for_up = (len(s) - 1) // columns
cuu = os.popen('tput cuu %s' % need_for_up).read()
print(s, end='\r' + cuu)
print(s1)因为这不适用于坐标,而是实际上将光标从它的位置向上移动,所以无论在底部还是在底部,它都能工作。
因为这也使用tput查询Terminfo数据库,所以它也不能在Windows上工作。如果您硬编码cuu = "\033[%sA" % need_for_up,它是否会工作,我不知道(不是一个Windows )。
https://stackoverflow.com/questions/53042154
复制相似问题