-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert.py
More file actions
62 lines (48 loc) · 1.74 KB
/
Copy pathconvert.py
File metadata and controls
62 lines (48 loc) · 1.74 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import json
import csv
from datetime import datetime
# Function to convert JSON to CSV
def json_to_csv(json_file):
# Load JSON data
with open(json_file, 'r') as f:
data = json.load(f)
# Extract relevant information
result = data['chart']['result'][0]
exchange_name = result['meta']['exchangeName']
timestamps = result['timestamp']
quotes = result['indicators']['quote'][0]
adjclose = result['indicators']['adjclose'][0]['adjclose']
# Open CSV file for writing
csv_file = f'{exchange_name}.csv'
with open(csv_file, 'w', newline='') as csvfile:
fieldnames = ['Date', 'Open', 'High', 'Low', 'Close', 'Adj Close', 'Volume']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
# Write the header
writer.writeheader()
# Write data rows
for i in range(len(timestamps)):
# Convert timestamp to date
date = datetime.utcfromtimestamp(timestamps[i]).strftime('%Y-%m-%d')
# Prepare data row
row = {
'Date': date,
'Open': quotes['open'][i],
'High': quotes['high'][i],
'Low': quotes['low'][i],
'Close': quotes['close'][i],
'Adj Close': adjclose[i],
'Volume': quotes['volume'][i]
}
# Write row to CSV
writer.writerow(row)
print(f"Data has been written to {csv_file}")
# Function to read from a CSV file
def read_csv(csv_file):
with open(csv_file, 'r') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
print(row) # Print each row as a dictionary
# File paths
json_file = 'json/cmx.json'
# Convert JSON to CSV
json_to_csv(json_file)