tick数据保存到csv

发布时间 2023-08-17 10:45:57作者: C羽言
def tick2csv(res):
    csvfiles = {}
    submap = {}
    csvheader = ['symbol', 'updateTime', 'lastPrice']
    for id in subID:
        csvname = f"./csv/{id}_tick.csv"
        if not os.path.exists(csvname):
            csvfile = open(csvname, 'w', newline='')
            csvfile_w = csv.writer(csvfile)
            csvfile_w.writerow(csvheader)
        else:
            csvfile = open(csvname, 'a', newline='')
            csvfile_w = csv.writer(csvfile)
        csvfiles[id] = csvfile
        submap[id] = csvfile_w
    for tick in res:
        submap[tick[1]].writerow([tick[1], tick[18], tick[2]])
        csvfiles[tick[1]].flush()