简体   繁体   English

python break 打印数据后不断关闭脚本

[英]python break keeps closing the script after printing data

I have a piece of code I trying to get working, it's supposed to print a list of trade history.我有一段代码我试图开始工作,它应该打印一个交易历史列表。 I only need it to print the last row.我只需要它来打印最后一行。 I was able to get that to work but, I also want it to loop every time.sleep(60)我能够让它工作,但我也希望它每次循环。sleep(60)

The break is needed in order for the correct data to print.为了打印正确的数据,需要中断。 unfortunately, it closes the script after printing the data.不幸的是,它在打印数据后关闭脚本。 any suggestions?有什么建议么?

exchange.load_markets ()

symbol = 'BTC/USDT'
from_id = '0'
params = { 'fromId': from_id }
previous_from_id = from_id

all_trades = []

while True:

    print('------------------------------------------------------------------')
    print('Fetching with params', params)
    trades = exchange.fetch_my_trades(symbol, None, None, params)
    print('Fetched', len(trades), 'trades')
    if len(trades):
        last_trade = trades[len(trades) - 1]
        if last_trade['id'] == previous_from_id:
            break
        else:
            params['fromId'] = last_trade['id']
            previous_from_id = last_trade['id']
            all_trades = all_trades + trades
    else:
        break

#print('Fetched', len(all_trades), 'trades')
for i in range(0, len(trades)):
    trade = trades[i]
    with open("recent.txt", "w") as f:
        f.write(str(trade['price']))
        print (trade['date'], trade['price'], trade['amount'])
        f.close()

Unfortunately I can't test your code, or additions to it, because it is not complete and self-contained .不幸的是,我无法测试您的代码或对它的补充,因为它不完整且不独立 Instead, here is a stripped-down demo program that mimics the same structure:相反,这是一个模拟相同结构的精简演示程序:

responses = []
print('Enter as many responses as you would like. Press enter on a blank line '
      'to see output.')

while True:
  response = input('Response: ')
  if response:
    responses.append(response)
  else:
    break

print('Output:')
for response in responses:
  print(f'***{response}***')

Like your actual program, this one creates an empty list, adds to it repeatedly in a while loop, and then, after breaking out of that loop, loops through the list in a for loop to print the output.就像您的实际程序一样,这个程序创建一个空列表,在 while 循环中重复添加,然后在退出该循环后,在 for 循环中循环遍历列表以打印 output。 Also like yours, the program exits as soon as it's done printing the output.和你的一样,程序在打印完 output 后立即退出。

To make the whole thing repeat, put it all inside an outer for loop.要使整个事情重复,请将其全部放在外部 for 循环中。 Putting a call to time.sleep at the end of this outer loop will cause it to pause in between.在这个外循环结束时调用time.sleep会导致它在两者之间暂停。

import time

while True:
  responses = []
  print('Enter as many responses as you would like. Press enter on a blank '
        'line to see output.')

  while True:
    response = input('Response: ')
    if response:
      responses.append(response)
    else:
      break

  print('Output:')
  for response in responses:
    print(f'***{response}***')

  time.sleep(3)

This will repeat indefinitely, until you halt it with Ctrl + C .这将无限重复,直到您使用Ctrl + C停止它。

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

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