0

I am trying to save the stock data I downloaded from a API using python. 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()

1 Answer 1

1

Borrowing from How do I write a Python dictionary to a csv file?

You have a nested dict structure. 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.

These are the contents of 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. It'll probably give you more ideas.


UPDATE AS PER OP'S COMMENT:

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:

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
Sign up to request clarification or add additional context in comments.

2 Comments

1030, 1130, 1400 and 1500 represents 10:00,11:30,14:00 and 15:00. These are the specific time point. Can you add them followed by the date? Thanks!
That diverts from the initial question and may be confusing for other people who find your question, but there it is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.