-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstruments.py
More file actions
50 lines (39 loc) · 1.4 KB
/
instruments.py
File metadata and controls
50 lines (39 loc) · 1.4 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
import os
import MySQLdb
# calls API to get data, renames the folder, and deletes the ZIP
os.system(
'''wget https://www.quandl.com/api/v3/databases/LSE/codes?
api_key=ZaNCWVQ2Gx8zMKMZwnHn'''
)
os.system('unzip codes')
os.system('mv LSE-datasets-codes.csv instruments.csv')
os.system('rm codes')
# connect to the database
db = MySQLdb.connect(
host="localhost",
user="georgegarber",
passwd="password",
db="marketanalysisdb")
cur = db.cursor()
# create the instruments table
query = '''create table if not exists instruments(instrumentid varchar(8)
primary key, description varchar(30), currency varchar(3));'''
cur.execute(query)
instruments = open("instruments.csv", "r")
# go through the list of downloaded instruments
for line in instruments:
# remove double quotes
line = line.replace('"', '')
# makes sure it has a price, and it has a currency
if line.find('price') == -1 or line.find('Currency') == -1:
continue
# extracts the useful information from the description
instrumentid = line[0:line.find(',')]
description = line[line.find(',') + 1:line.find('price') + 5]
currency = line[line.find('Currency ') + 9:line.find('Currency ') + 12]
# insert the data into the database
query = '''insert ignore into instruments values
("''' + instrumentid + '","' + description + '","' + currency + '");'
cur.execute(query)
db.commit()
db.close()