简体   繁体   English

如何每秒刷新 Python 中的 csv 导出?

[英]How to refresh a csv export in Python every second?

I have this code for python that looks up an API key and writes into a file the findings.我有 python 的代码,它查找 API 密钥并将结果写入文件。 How can I make this refresh the contents of the file every one second?我怎样才能让它每秒刷新一次文件的内容?

import requests
import csv

r = requests.get('https://api.kraken.com/0/public/Trades?pair=XBTUSD').json()

c = csv.writer(open("order_book", "w"), lineterminator='\n')

for item in r['result']['XXBTZUSD']:
    c.writerow(item)

Schedule ( https://pypi.python.org/pypi/schedule ) seemed to be what you need.时间表( https://pypi.python.org/pypi/schedule )似乎是你需要的。

You will have to install their Python library:您必须安装他们的 Python 库:

pip install schedule

then modify the sample script:然后修改示例脚本:

import schedule
import time

def job():

schedule.every(1).seconds.do(job)

while True:
    schedule.run_pending()
    time.sleep(1)

With your own function in job() and use the needed call for your timing.在 job() 中使用您自己的 function 并根据您的时间使用所需的调用。

Then you can run it with nohup.然后你可以用 nohup 运行它。

Be advised that yu will need to start it again if you reboot.请注意,如果您重新启动,您将需要重新启动它。

here is the docs这是文档

thank you!谢谢你! it worked!有效!

import requests
import csv
import schedule
import time


def job():
    r = requests.get('https://api.kraken.com/0/public/Trades?pair=XBTUSD').json()
    c = csv.writer(open("order_book", "w"), lineterminator='\n')
    for item in r['result']['XXBTZUSD']:
        c.writerow(item)


schedule.every(1).seconds.do(job)

while True:
    schedule.run_pending()
    time.sleep(1)

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

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