In Python, how to change text after it's printed?

后端 未结 5 738
名媛妹妹
名媛妹妹 2020-12-17 16:46

I have a Python program I am writing and I want it to be able to change text after it is printed. For example, let\'s say I want to print \"hello\" and erase one letter eve

5条回答
  •  萌比男神i
    2020-12-17 17:21

    Here's one way to do it.

    print 'hello',
    sys.stdout.flush()
    ...
    print '\rhell ',
    sys.stdout.flush()
    ...
    print '\rhel ',
    sys.stdout.flush()
    

    You can probably also get clever with ANSI escapes. Something like

    sys.stdout.write('hello')
    sys.stdout.flush()
    for _ in range(5):
        time.sleep(1)
        sys.stdout.write('\033[D \033[D')
        sys.stdout.flush()
    

提交回复
热议问题