tools.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:iqfeed 作者: tibkiss 项目源码 文件源码
def write_bars_to_file(bars, filename, tz):
    """Creates CSV file from list of Bar instances"""
    date_format_str = "%Y%m%d %H%M%S"

    rows = [{'DateTime':  bar.datetime.astimezone(tz).strftime(date_format_str),
             'Open':      bar.open,
             'High':      bar.high,
             'Low':       bar.low,
             'Close':     bar.close,
             'Volume':    bar.volume,
             } for bar in bars]

    if os.path.exists(filename):
        raise Exception("File already exists!")

    fd = os.popen("gzip > %s" % filename, 'w') if filename.endswith('.gz') else open(filename, 'w')

    with fd:
        csv_writer = csv.DictWriter(fd, ['DateTime', 'Open', 'High', 'Low', 'Close', 'Volume'])
        csv_writer.writeheader()
        csv_writer.writerows(rows)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号