Search code examples
pythonpython-3.xcsvdictionarystockquotes

How to format a list of dictionaries from CSV? - Python


I have a CSV file of stock price data that I would like to put into a dictionary containing the Date and Close price.

Here is what the CSV looks like: date close volume open high low 2017/09/22 151.89 46575410 152.02 152.27 150.56 2017/09/21 153.39 37350060 155.8 155.8 152.75 2017/09/20 156.07 52126240 157.9 158.26 153.83 2017/09/19 158.73 20565620 159.51 159.77 158.44

I would like the end dictionary to be arranged like this:

perfect_dict = [
{'Date': '2017/09/22', 'Close': '151.89'},
{'Date': '2017/09/21', 'Close': '153.39'},
...]

My current code grabs the CSV data and creates two separate lists for the dates and the close prices. I've tried using dict(zip(dates, close_prices) but that doesn't format the new dictionary the way I mentioned above. This is my code:

import csv
from collections import defaultdict

# --->
columns = defaultdict(list)

with open('mydata.csv') as f:
    reader = csv.DictReader(f) 
    for row in reader: value2,...}
        for (k,v) in row.items(): 
            columns[k].append(v) 

dates = columns['date']
close_prices = columns['close']

# This is what doesn't format it right
#stock_dict = dict(zip(dates, close_prices))
#pprint.pprint(stock_dict)

If anyone could point me in the right direction that would be awesome, thanks!


Solution

  • By using pandas to read the csv file

    • first read the date and close column and store as a list
    • than make a list of dictionary which format we needed.

    The code

    import pandas as pd
    df = pd.read_csv("file_name.csv")
    # read the date and close column and store as a list.
    time_list = list(df['date'])
    close_list = list(df['close'])
    perfect_dict = []
    # here take the minimum length
    # because avoiding index error
    take_length = min(len(time_list),len(close_list))
    for i in range(take_length):
        temp_dict={}
        temp_dict["Date"]=time_list[i]
        temp_dict["Close"] = close_list[i]
        perfect_dict.append(temp_dict)
    print(perfect_dict)
    

    The another possible way.

    import csv
    perfect_dict=[]
    with open('file.csv') as f:
        reader = list(csv.reader(f))
        for row in reader[1:]:
            temp_dict = {}
            temp_dict["Date"] = row[0]
            temp_dict["Close"] = row[1]
            perfect_dict.append(temp_dict)
    print(perfect_dict)