2017-01-30 196 views
0

運行下面的代碼後,你可以在基於您的選擇,它看起來像「指數」轉換單引號字符串中雙引號字符串

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() 

五項指標列表:

index 

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

現在我必須在下面的代碼末尾輸入這個字符串到另一行代碼,找到零件索引[0],索引[1] ...索引[4]

這不會發生。我的猜測是它必須是由於需要雙引號。 關於如何實現它的任何想法。嘗試替換和所有。

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'")))` 
+2

不明確的,你遇到的具體問題是什麼,但我認爲它肯定地說,該字符串的引號不是問題這裏。 – Sayse

+0

'select * from yahoo.finance.historicaldata where(index [0],index [1],index [2],index [3],index [4])and startDate ='+ x [i] +'和endDate ='+ pd.to_datetime(x [i + 1])。strftime(''%y-%m-%d'「)))' 所以我期待索引[0]應該被它的替換內容在上面。你是對的,因爲我不知道什麼是錯的,以及爲什麼'索引'列表值不在上面。 – Bhushan

回答

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

實際上並沒有做索引值的任意更換,因爲它只是把這個作爲文本字符串,而不是你需要至少適用於他們一些字符串格式化。

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

這應該至少讓你更接近正確的sql字符串。

雖然,你應該確保使用parameterized queries避免bobby tables

+1

謝謝Sayse! ({「,{」,「{}」,「{}」,「{}」)'中的符號。 (index [0]),index [1],index [2],index [3],index [4])+'and startDate ='+ x [i] +'and endDate ='+ pd.to_datetime(x [I + 1])。的strftime( 「%Y-%間 - %d」')))) – Bhushan

相關問題