如何在循环中更改数据库的名称?

时间:2018-01-29 14:14:13

标签: database python-3.x loops

我正在努力在循环中更改数据库的名称(第47-49行)。该程序正在做什么 - 它提示用户进入一个国家并自动从Quandl数据库下载股票指数的数据。但是,一旦我这样做,我想调用数据框,如" data_country"。请告诉我这个问题。

enter code here
import quandl                     #To extract the data from Quandl website
from quandl.errors.quandl_error import NotFoundError #for error handling 
from datetime import date, timedelta                 # to define the date and time, while "timedelta" - for 1 month back
import matplotlib.pyplot as plt      #Plotting library
import pandas as pd                  #Time series working library
from scipy import interpolate        #Scipy library for interpolation 
import numpy as np
from scipy.optimize import leastsq

def MainFormula():
exchange1, exchange2, exchange3, exchange4, exchange5  = [None]*5    
while exchange1 is None or exchange2  is None or exchange3  is None or  exchange4  is None or exchange5  is None:

    exchange1 = input('Please, choose a country from Germany, France, USA, HongKong, India, England, Japan or China: ')
    exchange2 = input('Please, choose a second country for comparison: ')
    exchange3 = input('Please, choose a third country for comparison: ')
    exchange4 = input('Please, choose a fourth country for comparison: ')
    exchange5 = input('Please, choose a fifth country for comparison: ')     
    exchange1 = str(exchange1)  #ensure that entered exchage is string type
    exchange2 = str(exchange2)  #ensure that entered exchage is string type
    exchange3 = str(exchange3)  #ensure that entered exchage is string type
    exchange4 = str(exchange4)  #ensure that entered exchage is string type
    exchange5 = str(exchange5)  #ensure that entered exchage is string type
    exchanges = [exchange1, exchange2, exchange3, exchange4, exchange5]       
    if any(e.lower() == 'germany' for e in exchanges):
        ticker = "CHRIS/EUREX_FDAX1"            
    elif any(e.lower() == 'france' for e in exchanges):
        ticker = "CHRIS/LIFFE_FCE1"              
    elif any(e.lower() == 'usa' for e in exchanges):
        ticker = "MULTPL/SP500_REAL_PRICE_MONTH"         
    elif any(e.lower() == 'hong kong' for e in exchanges):
        ticker = "CHRIS/HKEX_HSI1"            
    elif any(e.lower() == 'india' for e in exchanges):
        ticker = "NSE/CNX_NIFTY"            
    elif any(e.lower() == 'japan' for e in exchanges):
        ticker = "NIKKEI/ALL_STOCK"             
    elif any(e.lower() == 'england' for e in exchanges):
        ticker = "CHRIS/LIFFE_Z1"      
    elif any(e.lower() == 'china' for e in exchanges):
        ticker = "WFE/INDEXES_SHANGHAISESSECOMPOSITEINDEX"
    try:
        date_time = date.today() #define today's date
        ten_years = date_time - timedelta(days=365*10) # define the date one month ago
        end = date_time.strftime("%Y/%m/%d") #make sure that date is in Y-m-d format
        start = ten_years.strftime("%Y/%m/%d") #make sure that date is in Y-m-d format
        global data
        for i in exchanges:
            global data
            data[i] = quandl.get(ticker, start_date=start, end_date=end)
    except (SyntaxError, NotFoundError):
        " "
        print('Incorrect arguments. Please, try another country.')
        exchange3 = None                      
return "Here are the results"
program = MainFormula()               
print(program)  

0 个答案:

没有答案