简体   繁体   English

python:要覆盖多个打印行

[英]python: multiple print lines to be overwritten

Sorry for the noob question, I checked out other similar questions on the website but couldn't find one that solves my problem.抱歉这个菜鸟问题,我在网站上查看了其他类似的问题,但找不到解决我问题的问题。

I'm very new to python so forgive me.我对 python 很陌生,所以请原谅我。 I'm trying to make a bot which shows results of my trading account.我正在尝试制作一个显示我的交易账户结果的机器人。

First step I would like to be able to see results in the terminal.第一步我希望能够在终端中看到结果。 Second step will be "writing those results on a webpage"第二步将是“将这些结果写在网页上”

what i would like to achieve is that every single line of print get updated with new values on the same original lines.我想要实现的是,每一行打印都会在相同的原始行上使用新值进行更新。

the portion code of the function is this: 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')

then I'm telling it to repeat each 5 seconds然后我告诉它每 5 秒重复一次

while True:
  eth_results()
  time.sleep(5)

right now the output is like this but i would like to have the results printed in the same lines, updating just the variables :现在 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

--------------------------------------

but I can't achieve it.但我无法实现。 If i use the \r only the last line will be replaced.如果我使用\r只有最后一行将被替换。 Basically I would like to replace ALL the lines.基本上我想替换所有的行。 I'm pretty sure there are multiple ways to achieve this, and multiple ways to "clean the code" but right now I'm just willing to find ANY "working solution" rather than finding "the only best way" to do it.我很确定有多种方法可以实现这一目标,并且有多种方法可以“清理代码”,但现在我只想找到任何“可行的解决方案”,而不是找到“唯一最好的方法”来做到这一点。 Of course I'm willing to code in a better way when I'll gain more experience.当然,当我获得更多经验时,我愿意以更好的方式编写代码。

BONUS : after that I would also like to basically print (and ovewrite) the same thing on a text/html/whatever kind of file.奖励:在那之后,我还想基本上在文本/html/任何类型的文件上打印(和覆盖)相同的东西。 But this is not the priority now.但这不是现在的优先事项。

Thanks a lot非常感谢

You can print a bunch of new lines to clear the console.您可以打印一堆新行来清除控制台。

clear = "\n" * 100
print(clear)

You cannot overwrite an already printed line in the console since it is an output.您不能覆盖控制台中已打印的行,因为它是 output。

You can use the following您可以使用以下

import os
print('text')
os.system("clear")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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