简体   繁体   中英

How to overwrite previous printed text?

I have looked around at some of the questions already asked but none of them seem to fit my situation. So I have a very basic program the shows me the current value of bitcoin:

import requests
import os
import sys
import time
while True:
    main_api = ('https://api.coindesk.com/v1/bpi/currentprice.json')
    json_data = requests.get(main_api).json()
    json_updated = json_data['time']['updated']
    json_value = json_data['bpi']['USD']['rate']
    time.sleep(1)
    print('\n' 'Last Updated: ' + json_updated)
    print('\n' "Bitcoin price: " + json_value + " USD")

and it works well for the most part. But there is a small problem, every time the code executes itself (every second) it will create more text in the terminal displaying the currency and the last time it was updated. But It will not remove the previous text which makes it look ugly and a bit confusing so I aim to make it look like, in the terminal, that there is only once instance of the text that updates itself, giving a much more cleaner look.

I have seen some solutions like:

for x in range(10):
    print(x, end='\r')
print()

from How to overwrite the previous print to stdout in python?

and

import time
for x in range (0,5):  
    b = "Loading" + "." * x
    print (b, end="\r")
    time.sleep(1)

from Remove and Replace Printed items

But I honestly have no idea how or IF I can incorporate those solutions into my own code since my program is much more different from the ones used in the solutions or I am just I noob, probably the latter lol.

Thanks.

This is a parcial solution, because there is no simple way of including a newline.

import sys
import time
import requests
while True:
    main_api = ('https://api.coindesk.com/v1/bpi/currentprice.json')
    json_data = requests.get(main_api).json()
    json_updated = json_data['time']['updated']
    json_value = json_data['bpi']['USD']['rate']
    b ="Bitcoin price: " + json_value + " USD"
    sys.stdout.write('\r' + b)
    time.sleep(1)

Here is a solution that updates both time, and currency. It's by simply clearing the terminal window before each time you get updated data.

import requests
import os
import sys
import time
def clear():
    if sys.platform=="win32":
        os.system("cls") # cmd clear command for Windows systems
    elif sys.platform in ["linux", "darwin"]: 
        os.system("clear") # terminal clear command for Linux and Mac OS
    else:
        raise OSError("Uncompatible Operating-System.")
while True:
    main_api = ('https://api.coindesk.com/v1/bpi/currentprice.json')
    json_data = requests.get(main_api).json()
    json_updated = json_data['time']['updated']
    json_value = json_data['bpi']['USD']['rate']
    time.sleep(1)
    clear()
    print('\n' 'Last Updated: ' + json_updated)
    print('\n' "Bitcoin price: " + json_value + " USD")

You can also add a line for the current hour if it looks freezed.

import requests
import os
import sys
import time
from datetime import datetime
def clear():
    if sys.platform=="win32":
        os.system("cls") # cmd clear command for Windows systems
    elif sys.platform in ["linux", "darwin"]: 
        os.system("clear") # terminal clear command for Linux and Mac OS
    else:
        raise OSError("Uncompatible Operating-System.")
while True:
    main_api = ('https://api.coindesk.com/v1/bpi/currentprice.json')
    json_data = requests.get(main_api).json()
    json_updated = json_data['time']['updated']
    json_value = json_data['bpi']['USD']['rate']
    time.sleep(1)
    clear()
    print('\n' "Current date and time :", str(datetime.now())[:-7])
    print('\n' 'Last Updated: ' + json_updated)
    print('\n' "Bitcoin price: " + json_value + " USD")

It should help, unless you don't want your screen cleared.

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