简体   繁体   English

如何在python中将实时数据写入csv文件

[英]how to write the real time data into csv file in python

def event_handler_quote_update(message):
    # print(f"quote update {message}")
    global row_no
    global token
    global ltp
    global Date
    token = message['token']
    ltp =   message['ltp']
    Date = message['exchange_time_stamp']

i need to write the above live running data into csv file currently my python script will not do anything writing the csv file.我需要将上述实时运行数据写入 csv 文件,目前我的 python 脚本不会执行任何写入 csv 文件的操作。

currently it is working with excel file, but i am having issue while reading same excel from different script, so i am planning to write the data into csv file,目前它正在使用 excel 文件,但是我在从不同的脚本读取相同的 excel 时遇到问题,所以我打算将数据写入 csv 文件,

There is no need to declare any of those things global .没有必要将这些东西声明为global A global declaration is useful and necessary when there is a variable outside of your def which you need to change from inside.当您需要从内部更改def之外的变量时, global声明是有用且必要的。

For writing CSV data, probably use the csv library.对于写入 CSV 数据,可能使用csv库。

import csv

# ...

def event_handler_quote_update(message):
    # print(f"quote update {message}")
    token = message['token']
    ltp   = message['ltp']
    date  = message['exchange_time_stamp']
    with open('results.csv', 'a+') as csvfile:
        writer = csv.writer(csvfile)
        writer.writerow([token, ltp, date])

It would be a lot better if there was a way to open the CSV file for writing and instantiate the writer object just once, outside the callback, and simply pass in writer as the second argument after message ;如果有一种方法可以在回调之外打开 CSV 文件进行写入和实例化writer对象一次,并且只需将writer作为message之后的第二个参数传入; though then, you will need to close() it separately when you are done.尽管如此,您需要在完成后单独close()它。

import csv

# ...

def event_handler_quote_update(message, writer):
    # print(f"quote update {message}")
    token = message['token']
    ltp   = message['ltp']
    date  = message['exchange_time_stamp']
    writer.writerow([token, ltp, date])

# ...

if __name__ == '__main__':
    # whatever else you have here
    csvfile = open('results.csv', 'w')
    writer = csv.writer(csvfile)

    # ... whatever else your program needs to do

    csvfile.close()

But maybe you don't have control over how the callback gets called.但也许您无法控制回调的调用方式。 Then maybe you do need writer to be global inside the def .那么也许你确实需要writerdefglobal的。

There's no need really to copy each field into a separate variable;没有必要将每个字段复制到一个单独的变量中; if there are a lot of fields you want to extract, probably switch to something like如果你想提取很多字段,可能切换到类似的东西

        writer.writerow([message[x] for x in ('token', 'ltp', 'exchange_time_stamp')])

See the documentation link above if you need to use a different variant of CSV format, like tab or semicolon delimited, or write a header at the top, etc. It's all there.如果您需要使用不同的 CSV 格式变体,如制表符或分号分隔,或在顶部写入标题等,请参阅上面的文档链接。一切都在那里。

convert your data into csv formate and pass into below function.将您的数据转换为 csv 格式并传递到以下函数。

tempstr = token + ";" + ltp + ";" + Date + "\n"
write_file = "output.csv"
with open(write_file, "w") as output:
    output.write(tempstr)

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

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