Python newbie here.
I have a bunch of CSV files of stock prices. They are structured as date, opening, high, low, close, adj. close, volume. I only need the date (column 1), the opening (column 2) and close (column 5).
The task prohibits the use of pandas. How can I extract those three columns and return them?
This is my attempt, but that doesn’t extract the 3 columns I need.
def read_daily_prices(daily_price): daily_prices = [] with open(stocks_symbols + ".csv", "r", newline='') as csvfile: csvreader = csv.DictReader(csvfile, delimiter=',') for daily_price in csvreader: daily_prices.append(daily_price)
Advertisement
Answer
Hi👋🏻 Hope you are doing well!
If I understood your question correctly, you can do something like that:
import csv path = "stocks.csv" # stocks.csv # date, opening, high, low, close, adj_close, volume # 2022-01-01, opening_1, high_1, low_1, close_1, adj_close_1, volume_1 # 2022-01-02, opening_2, high_2, low_2, close_2, adj_close_2, volume_2 columns = ["date", "opening", "close"] daily_prices = [] with open(path, newline="") as csvfile: reader = csv.DictReader(csvfile, delimiter=",") for row in reader: daily_prices.append([row["date"], row["opening"], row["close"]]) print(daily_prices) # [['2022-01-01', 'opening_1', 'close_1'], ['2022-01-02', 'opening_2', 'close_2']] daily_prices_dicts = [dict(zip(columns, row)) for row in daily_prices] print(daily_prices_dicts) # [{'date': '2022-01-01', 'opening': 'opening_1', 'close': 'close_1'}, {'date': '2022-01-02', 'opening': 'opening_2', 'close': 'close_2'}]
the main idea is: csv.DictReader
returns list of dicts so you can select necessary columns by key. 🙂