简体   繁体   中英

How do I overwrite a line in Python

I have created a simple python program that will count up to 10

What I am trying to achieve is for the program to delete the previous number and print the new number

This is the code that I have created:

import sys
import time
for i in range(10):
    text = "\r" + str(i)
    sys.stdout.write(text)
    sys.stdout.flush()
    time.sleep(1)

Which outputs:

0123456789

Changing the code to have the "\\r" after the str(i) didn't work either

import sys
import time
for i in range(10):
    text = str(i) + "\r"
    sys.stdout.write(text)
    sys.stdout.flush()
    time.sleep(1)

Which also resulted in:

0123456789

I was looking for it to count up, and display at the end only the 9 however it doesn't overwrite the previous numbers

Edit:

I am using Windows, Python3

Edit 2:

How to overwrite the previous print to stdout in python? Does not give me a working answer, it still doesn't do what I want

Therefore due to my excellent reasoning it is not a duplicate :P

或者,您可以使用os.system("cls")清除整个窗口

There are some control symbols accepted by virtual terminals. One of them is '\\b' that moves a carret for one place back. This one is accepted on Windows too and I will use it in my example below. Unix terminals accept a lot more of controls including color changes and more.



from time import sleep
import sys, os

def clear ():
    os.system("cls" if sys.platform.startswith("win") else "clear")

clear()

s = "1"
sys.stdout.write(s)
for x in range(2, 21):
    sleep(1)
    # Return carret to beginning of line:
    l = len(s)
    s = l*"\b"
    sys.stdout.write(s)
    # Clear line (just in case):
    s = l*" "
    sys.stdout.write(s)
    # Return to the beginning again:
    s = l*"\b"
    sys.stdout.write(s)
    # Write over new text:
    s = str(x)
    sys.stdout.write(s)

Use the end parameter to specify a "\\r" as the line ending.

import time
for i in range(10):
    print(i, end="\r")
    time.sleep(1)

I would do it like this:

for x in range(10):
    print("{}".format(x), end="\r")

Why not use the standard print of python? Althougth given in many debugger screens this tends to not work. In terminal (ubuntu) it does

import time

for i in range (10):
    print(i, end='\r')
    time.sleep(1)

Probably for stdout something exists too but in this case I usually use the print of python3

I believe your code should work perfectly fine. The problem should be that you are using a Windows terminal . Try using Linux. Most code does not work in Windows. You can refer this link to know why you have to stop using python on Windows.

This is also an alternate code you can try.

import time
    for i in range(10):
        text = str(i)
        print(text,end = "\r")
        time.sleep(1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM