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.
JavaScript
x
7
1
def read_daily_prices(daily_price):
2
daily_prices = []
3
with open(stocks_symbols + ".csv", "r", newline='') as csvfile:
4
csvreader = csv.DictReader(csvfile, delimiter=',')
5
for daily_price in csvreader:
6
daily_prices.append(daily_price)
7
Advertisement
Answer
Hi👋🏻 Hope you are doing well!
If I understood your question correctly, you can do something like that:
JavaScript
1
25
25
1
import csv
2
3
path = "stocks.csv"
4
# stocks.csv
5
# date, opening, high, low, close, adj_close, volume
6
# 2022-01-01, opening_1, high_1, low_1, close_1, adj_close_1, volume_1
7
# 2022-01-02, opening_2, high_2, low_2, close_2, adj_close_2, volume_2
8
9
columns = ["date", "opening", "close"]
10
11
daily_prices = []
12
13
with open(path, newline="") as csvfile:
14
reader = csv.DictReader(csvfile, delimiter=",")
15
for row in reader:
16
daily_prices.append([row["date"], row["opening"], row["close"]])
17
18
print(daily_prices)
19
# [['2022-01-01', 'opening_1', 'close_1'], ['2022-01-02', 'opening_2', 'close_2']]
20
21
daily_prices_dicts = [dict(zip(columns, row)) for row in daily_prices]
22
23
print(daily_prices_dicts)
24
# [{'date': '2022-01-01', 'opening': 'opening_1', 'close': 'close_1'}, {'date': '2022-01-02', 'opening': 'opening_2', 'close': 'close_2'}]
25
the main idea is: csv.DictReader
returns list of dicts so you can select necessary columns by key. 🙂