[英]How to save stock data to csv file using python
I am trying to save the stock data I downloaded from a API using python. 我正在尝试保存使用python从API下载的股票数据。 But I have no idea about how to do this.
但是我不知道该怎么做。
The data is printed out as below: 数据打印如下:
>>>
{'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}},....
My last several lines of the code is: 我的最后几行代码是:
def main():
g = getStock60MIN('000030', 'sz')
print g
if __name__ == "__main__":
main()
Borrowing from How do I write a Python dictionary to a csv file? 从如何将Python字典写入csv文件中借用?
You have a nested dict structure. 您具有嵌套的dict结构。 Maybe the following will help:
也许以下方法会有所帮助:
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)
I don't know what do you want to do with your secondary dictionary keys, though (the 1030
, 1130
, 1400
and 1500
) My method disregards them. 我不知道你想用辅助字典键做什么,但(在
1030
, 1130
, 1400
和1500
),我的方法忽略它们。
These are the contents of mycsvfile.csv
: 这些是
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
Take a look to the csv module in Python. 看一下Python中的csv模块。 It'll probably give you more ideas.
它可能会给您更多的想法。
UPDATE AS PER OP'S COMMENT: 根据OP的评论进行更新:
Since the keys of the first dictionary are dates and the keys of the second dictionary are times, a datetime.datetime
instance can be instanciated and then written in the csv file: 由于第一个字典的键是日期,而第二个字典的键是时间,因此可以实例化
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)
This produces the following output: 这将产生以下输出:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.