简体   繁体   English

将单引号字符串转换为双引号字符串

[英]Convert Single Quote String to Double Quote String

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] 现在,我必须将此字符串输入到以下代码末尾的另一行代码中,查找零件索引[0],索引[1] ...索引[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'") ))`
'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. 至少应该使您更接近正确的sql字符串。

Although, you should make sure to use parameterized queries to avoid bobby tables 虽然,您应该确保使用参数化查询来避免Bobby表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM