Repository navigation
Expand file tree
/
Copy pathload_data.py
More file actions
40 lines (35 loc) · 1.45 KB
/
Copy pathload_data.py
File metadata and controls
40 lines (35 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import csv
import numpy as np
import os
def create_close_price_array(file_path):
"""
Read CSV at file_path, extract the 5th column (index 4) as floats (closing prices),
save './close_prices.npy' and return the numpy array.
This version does NOT append to an existing CSV file; it builds the array in memory
and overwrites the .npy file each time so runs are deterministic.
"""
close_prices = []
with open(file_path, 'r', newline='') as file:
reader = csv.reader(file)
for row in reader:
if not row:
continue
# guard against short rows or headers
try:
val = float(row[4])
except (IndexError, ValueError):
# skip headers or malformed rows
continue
close_prices.append(val)
close_prices = np.array(close_prices, dtype=float)
# Save the array to .npy so other parts of the code that expect the file still work
np.save('./close_prices.npy', close_prices)
return close_prices
# keep this helper if you need to create a numpy array directly from a csv file
def create_close_price_numpy_array(file_path):
close_prices = np.loadtxt(file_path, delimiter=',')
np.save('./close_prices.npy', close_prices)
return close_prices
if __name__ == "__main__":
# example usage - replace path with your CSV path
create_close_price_array('/home/destiny/Programming/Kraken_OHLCVT/1INCHEUR_1440.csv')