【问题标题】:How to save stock data to csv file using python如何使用python将股票数据保存到csv文件
【发布时间】:2014-04-06 00:46:18
【问题描述】:

我正在尝试使用 python 保存从 API 下载的股票数据。但我不知道该怎么做。

数据打印如下:

>>> 
{'20140311': {'1030': {'total_price': 9421626, 'end': 6.76, 'high': 6.85, 'start': 6.78, 'low': 6.67, 'volumn': 1396431}, '1130': {'total_price': 5042807, 'end': 6.86, 'high': 6.91, 'start': 6.76, 'low': 6.76, 'volumn': 735220}, '1400': {'total_price': 5410292, 'end': 6.79, 'high': 6.9, 'start': 6.88, 'low': 6.76, 'volumn': 792890}, '1500': {'total_price': 6470290, 'end': 6.83, 'high': 6.85, 'start': 6.79, 'low': 6.74, 'volumn': 954111}},....

我最后几行代码是:

def main():
    g = getStock60MIN('000030', 'sz')
    print g

if __name__ == "__main__":
    main()

【问题讨论】:

    标签: python api stock


    【解决方案1】:

    借用How do I write a Python dictionary to a csv file?

    你有一个嵌套的 dict 结构。也许以下内容会有所帮助:

    import csv
    
    my_dict = {
      '20140311': {
        '1030': {
          'total_price': 9421626, 'end': 6.76, 'high': 6.85, 'start': 6.78, 'low': 6.67, 'volumn': 1396431
        },
        '1130': {
          'total_price': 5042807, 'end': 6.86, 'high': 6.91, 'start': 6.76, 'low': 6.76, 'volumn': 735220
        },
        '1400': {
          'total_price': 5410292, 'end': 6.79, 'high': 6.9, 'start': 6.88, 'low': 6.76, 'volumn': 792890
        }, 
        '1500': {
          'total_price': 6470290, 'end': 6.83, 'high': 6.85, 'start': 6.79, 'low': 6.74, 'volumn': 954111
        }
      }
    }
    
    def initialize_keys():
      for dict1 in my_dict.itervalues():
        for dict2 in dict1.itervalues():
          return dict2.keys()
    
    with open('mycsvfile.csv', 'wb') as f:
      w = csv.DictWriter(f, initialize_keys())
      w.writeheader()
      for dict1 in my_dict.itervalues():
        for dict2 in dict1.itervalues():
          w.writerow(dict2)
    

    我不知道你想用你的辅助字典键做什么,虽然(1030113014001500)我的方法忽略了它们。

    这些是mycsvfile.csv的内容:

    total_price,end,high,start,low,volumn
    9421626,6.76,6.85,6.78,6.67,1396431
    5042807,6.86,6.91,6.76,6.76,735220
    5410292,6.79,6.9,6.88,6.76,792890
    6470290,6.83,6.85,6.79,6.74,954111
    

    看看 Python 中的 csv 模块。它可能会给你更多的想法。


    根据 OP 的评论更新:

    由于第一个字典的键是日期,第二个字典的键是时间,所以可以实例化 datetime.datetime 实例,然后将其写入 csv 文件:

    import csv
    import datetime
    
    my_dict = {
      '20140311': {
        '1030': {
          'total_price': 9421626, 'end': 6.76, 'high': 6.85, 'start': 6.78, 'low': 6.67, 'volumn': 1396431
        },
        '1130': {
          'total_price': 5042807, 'end': 6.86, 'high': 6.91, 'start': 6.76, 'low': 6.76, 'volumn': 735220
        },
        '1400': {
          'total_price': 5410292, 'end': 6.79, 'high': 6.9, 'start': 6.88, 'low': 6.76, 'volumn': 792890
        }, 
        '1500': {
          'total_price': 6470290, 'end': 6.83, 'high': 6.85, 'start': 6.79, 'low': 6.74, 'volumn': 954111
        }
      }
    }
    
    def initialize_keys():
      for dict1 in my_dict.itervalues():
        for dict2 in dict1.itervalues():
          return dict2.keys()
    
    with open('mycsvfile.csv', 'wb') as f:
      w = csv.DictWriter(f, ['datetime'] + initialize_keys())
      w.writeheader()
      for datestr, dict1 in my_dict.iteritems():
        for timestr, dict2 in dict1.iteritems():
          whole_row = {}
          whole_row['datetime'] = datetime.datetime\
                    .strptime(datestr + timestr, '%Y%m%d%H%M')\
                    .strftime('%Y/%m/%d %H:%M')
          whole_row.update(dict2)
          w.writerow(whole_row)
    

    这会产生以下输出:

    datetime,total_price,end,high,start,low,volumn
    2014/03/11 10:30,9421626,6.76,6.85,6.78,6.67,1396431
    2014/03/11 11:30,5042807,6.86,6.91,6.76,6.76,735220
    2014/03/11 14:00,5410292,6.79,6.9,6.88,6.76,792890
    2014/03/11 15:00,6470290,6.83,6.85,6.79,6.74,954111
    

    【讨论】:

    • 1030、1130、1400 和 1500 代表 10:00、11:30、14:00 和 15:00。这些是具体的时间点。您可以在日期后添加它们吗?谢谢!
    • 这偏离了最初的问题,可能会让其他找到您问题的人感到困惑,但确实如此。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-11
    • 2016-09-01
    • 1970-01-01
    • 2017-09-24
    相关资源
    最近更新 更多