[英]python: multiple print lines to be overwritten
抱歉这个菜鸟问题,我在网站上查看了其他类似的问题,但找不到解决我问题的问题。
我对 python 很陌生,所以请原谅我。 我正在尝试制作一个显示我的交易账户结果的机器人。
第一步我希望能够在终端中看到结果。 第二步将是“将这些结果写在网页上”
我想要实现的是,每一行打印都会在相同的原始行上使用新值进行更新。
function 的部分代码是这样的:
def eth_results():
eth = s.get_wallet_balance()['result']['ETH']
eth_starting_balance = 0.05015276
eth_balance = eth['wallet_balance']
eth_equity = eth['equity']
eth_daily_pnl = format(eth['realised_pnl'],'.8f')
eth_percentage_pnl = eth_starting_balance * (eth_starting_balance / (eth_balance - eth_starting_balance))
print('\n--------------------------------------\n')
print(f'Coin: ETHEREUM')
print(f'Balance: {eth_balance}')
print(f'Starting Balance: {eth_starting_balance}')
print(f'Total Profit: {eth_percentage_pnl}')
print(f'Total Equity: {eth_equity}')
print('\n--------------------------------------\n')
然后我告诉它每 5 秒重复一次
while True:
eth_results()
time.sleep(5)
现在 output 是这样的,但我想在同一行打印结果,只更新变量:
--------------------------------------
Coin: ETHEREUM
Balance: 0.0513822
Starting Balance: 0.05015276
Total Profit: 2.0458902716827083
Total Equity: 0.05133733
--------------------------------------
--------------------------------------
Coin: ETHEREUM
Balance: 0.0513822
Starting Balance: 0.05015276
Total Profit: 2.0458902716827083
Total Equity: 0.05133754
--------------------------------------
--------------------------------------
Coin: ETHEREUM
Balance: 0.0513822
Starting Balance: 0.05015276
Total Profit: 2.0458902716827083
Total Equity: 0.05133706
--------------------------------------
但我无法实现。 如果我使用\r只有最后一行将被替换。 基本上我想替换所有的行。 我很确定有多种方法可以实现这一目标,并且有多种方法可以“清理代码”,但现在我只想找到任何“可行的解决方案”,而不是找到“唯一最好的方法”来做到这一点。 当然,当我获得更多经验时,我愿意以更好的方式编写代码。
奖励:在那之后,我还想基本上在文本/html/任何类型的文件上打印(和覆盖)相同的东西。 但这不是现在的优先事项。
非常感谢
您可以打印一堆新行来清除控制台。
clear = "\n" * 100
print(clear)
您不能覆盖控制台中已打印的行,因为它是 output。
您可以使用以下
import os
print('text')
os.system("clear")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.