0

After running following code, you can get list of five indexes in 'index'

import tkinter as tk
OPTIONS = [ "^AEX","^AXJO","^BFX","^BSESN","^MERV","^DJR","^PUT","^OMXC20","^DJA","^DJI","^DJICA","^DJT","^DJU","^DWCF"]
root = tk.Tk()
tk.Label(root, text='OptionMenus', bg='#aaa').pack(fill='x')
index = []
def on_button():
    index.clear()
    for i,var in enumerate(o_vars):
        index.append('{}'.format(var.get()))
        index.__str__()
        print('Selected Index {}: {}'.format(i+1, var.get()))
    print()
o_vars = []
for i in range(5):
    var = tk.StringVar(value='- select -')
    o_vars.append(var)
    o = tk.OptionMenu(root, var, *OPTIONS)
    o.pack()
b = tk.Button(root, text='OK', command=on_button)
b.pack(fill='x')
root.mainloop()

Based on your selection it looks like:

index

Out: ['^BSESN', '^DJI', '^HSI', '^JKII', '^KS11']

Now I have to input this string to another code of line at the end of following code, look for the part index[0],index[1]...index[4]

This is not happening. My guess is it must be due to required double quotation. Any idea on how to achieve it. Tried Replace and all.

import pandas as pd
import urllib
import datetime
import requests
import pylab
x =[]
yql_bs_query = []
yql_bs_url = []
data=[]
quote_new =[]
baseurl = "https://query.yahooapis.com/v1/public/yql?"
from datetime import date
import numpy as np
start_date = date(2007, 1, 1)
end_date = datetime.date.today()
delta = datetime.timedelta(np.timedelta64(end_date - start_date,'D').astype(int) /20)
while start_date <= end_date:
    x.append(start_date.strftime("%Y-%m-%d"))
    start_date += delta
for i in range(0,20):
    x[i] = str.format((pd.to_datetime(x[i]) + datetime.timedelta(days=1)).strftime("'%Y-%m-%d'"))
    yql_bs_query.append(('select * from yahoo.finance.historicaldata where symbol in (index[0],index[1],index[2],index[3],index[4]) and startDate = ' +x[i]+' and endDate = ' +pd.to_datetime(x[i+1]).strftime("'%Y-%m-%d'") ))`
Shane
  • 2,231
  • 1
  • 13
  • 17
Bhushan
  • 87
  • 1
  • 9
  • 2
    Its unclear what the exact problem you're having is, but i think its pretty safe to say that the quotes for the string isn't the issue here. – Sayse Jan 30 '17 at 10:30
  • 'select * from yahoo.finance.historicaldata where symbol in (index[0],index[1],index[2],index[3],index[4]) and startDate = ' +x[i]+' and endDate = ' +pd.to_datetime(x[i+1]).strftime("'%Y-%m-%d'") ))` so I am expecting index[0] should get replaced by its content in above line. you're right as I am not sure what's wrong either and why 'index' list values are not coming in above line. – Bhushan Jan 30 '17 at 10:57

1 Answers1

0
'select * from yahoo.finance.historicaldata where symbol in (index[0],index[1],index[2],index[3],index[4]) and startDate = '

Doesn't actually do any replacing of the index values since it just treats this as the literal strings, instead you need to at least apply some string formating on them.

'select * from yahoo.finance.historicaldata where symbol in ({}, {}, {}, {}, {}) and startDate = '.format(index[0],index[1],index[2],index[3],index[4])

Which should at least get you closer to the correct sql string.

Although, you should make sure to use parameterized queries to avoid bobby tables

Community
  • 1
  • 1
Sayse
  • 42,633
  • 14
  • 77
  • 146
  • 1
    Thank you Sayse! yql_bs_query.append((('select * from yahoo.finance.historicaldata where symbol in ("{}", "{}", "{}", "{}", "{}")'.format(str(index[0]),index[1],index[2],index[3],index[4]) +' and startDate = ' +x[i]+' and endDate = ' +pd.to_datetime(x[i+1]).strftime("'%Y-%m-%d'")))) – Bhushan Jan 30 '17 at 15:16