Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:循环遍历变量列表_Python_Python 2.7_Yahoo Finance - Fatal编程技术网

Python:循环遍历变量列表

Python:循环遍历变量列表,python,python-2.7,yahoo-finance,Python,Python 2.7,Yahoo Finance,我需要在我正在构建的URL中循环使用多个ticker符号。我不确定这是不是最好的办法 Yahoo财务url生成器 从日期时间导入日期时间 def ConstructyURL(股票代码、开始日期、结束日期、频率): 开始日期=datetime.strtime(开始日期,%Y-%m-%d”).date() end_date=datetime.strtime(end_date,“%Y-%m-%d”).date() s=ticker.replace(“^”和“%5E”) 如果开始日期.month-1我不

我需要在我正在构建的URL中循环使用多个
ticker
符号。我不确定这是不是最好的办法

Yahoo财务url生成器 从日期时间导入日期时间

def ConstructyURL(股票代码、开始日期、结束日期、频率): 开始日期=datetime.strtime(开始日期,%Y-%m-%d”).date() end_date=datetime.strtime(end_date,“%Y-%m-%d”).date()

s=ticker.replace(“^”和“%5E”)

如果开始日期.month-1我不确定
ConstructyURL()
应该做什么,但您可以使用
x
引用
ticker
中的每个条目:

ticker = ['fb', 'c', 'goog']
for x in ticker:
    yfURL = constructYFURL(x, start_date, end_date, freq)
我建议将每个URL构建为:

current_url = x + '/' + start_date + '/' + end_date + '/' + freq
这将在循环的第一次迭代中产生:

fb/2015-01-01/2017-02-23/d


但这取决于您希望URL的格式。

这将帮助您更接近URL
urlOfFile
未定义,并且
download()
不是python模块,因此您必须修复该问题。现在,如果您注释掉这些内容,它将正确构建URL

import urllib2
from datetime import datetime

def constructYFURL(x, start_date, end_date, freq): 
    start_date = datetime.strptime(start_date, '%Y-%m-%d').date()
    end_date = datetime.strptime(end_date, '%Y-%m-%d').date()
    s = x.replace('^', '%5E')

    if (start_date.month - 1) < 10:
        a = '0' + str(start_date.month - 1)
    else:
        a = str(start_date.month - 1)

    b = str(start_date.day)
    c = str(start_date.year)

    if (end_date.month - 1) < 10:
        d = '0' + str(end_date.month - 1)
    else:
        d = str(end_date.month - 1)

    e = str(end_date.day)
    f = str(end_date.year)
    g = freq

    yfURL = 'http://real-chart.finance.yahoo.com/table.csv?s='+s+'&a='+a+'&b='+b+'&c='+c+'&d='+d+'&e='+e+'&f='+f+'&g='+g+'&ignore=.csv'
    return yfURL

def main():
    webRequest = urllib2.Request(urlOfFile)
    try:
        page = urllib2.urlopen(webRequest)
        content = page.read()
        with open(filePath,'wb') as output:
            output.write(bytearray(content))   
    except urllib2.HTTPError as e:
        print e.fp.read()

    ticker = ['fb', 'c', 'goog']
    start_date = '2015-01-01'
    end_date = '2017-02-23'
    freq = 'd'
    localFilePath = '/Users/student/Downloads/datalibrary/'

    for x in ticker:
        yfURL = constructYFURL(x, start_date, end_date, freq)
        fileName = 'YF_' + x + '.csv'
        print yfURL    
        download(localFilePath + fileName, yfURL)

if __name__ == '__main__':
    main()
导入urllib2
从日期时间导入日期时间
def ConstructyURL(x,开始日期,结束日期,频率):
开始日期=datetime.strtime(开始日期,%Y-%m-%d')。日期()
end_date=datetime.strtime(end_date,%Y-%m-%d').date()
s=x.replace('^','%5E')
如果(开始日期月-1)<10:
a='0'+str(开始日期.月份-1)
其他:
a=str(开始日期月-1)
b=str(开始日期日)
c=str(开始日期年)
如果(月底-1)<10:
d='0'+str(结束日期月-1)
其他:
d=str(月底-1)
e=str(结束日期日)
f=str(截止日期年)
g=频率
yfull='1〕http://real-chart.finance.yahoo.com/table.csv?s=“+s+”&a='+a+'&b='+b+'&c='+c+'&d='+d+'&e='+e+'&f='+f+'&g='+g+'&ignore=.csv”
返回yURL
def main():
webRequest=urllib2.Request(urlofile)
尝试:
page=urlib2.urlopen(webRequest)
content=page.read()
以open(filePath,'wb')作为输出:
输出写入(字节数组(内容))
除了urllib2.HTTPError作为e:
打印e.fp.read()
股票代码=['fb'、'c'、'goog']
开始日期='2015-01-01'
结束日期='2017-02-23'
频率='d'
localFilePath='/Users/student/Downloads/datalibrary/'
对于股票代码中的x:
yfURL=constructYFURL(x,开始日期,结束日期,频率)
文件名='YF_'+x+'.csv'
打印yURL
下载(localFilePath+fileName,yfull)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
main()

在传递到构造时,是否应该使用变量“x”而不是ticker。。。功能?另外,请正确缩进您的代码,这对于Python示例很重要。请使您的代码可读。此代码即使可读也没有意义。您可以循环使用
ticker
,但从不使用
x
。相反,您使用的是
ticker
本身。本质上,我只想获取一个ticker的列表,并将它们逐一运行到我的URL中。理想情况下,我希望使用ticker变量,将其作为字符串列表(ticker),并将这些字符串逐个放入URL。您会收到什么错误消息?您的功能定义在哪里?
import urllib2
from datetime import datetime

def constructYFURL(x, start_date, end_date, freq): 
    start_date = datetime.strptime(start_date, '%Y-%m-%d').date()
    end_date = datetime.strptime(end_date, '%Y-%m-%d').date()
    s = x.replace('^', '%5E')

    if (start_date.month - 1) < 10:
        a = '0' + str(start_date.month - 1)
    else:
        a = str(start_date.month - 1)

    b = str(start_date.day)
    c = str(start_date.year)

    if (end_date.month - 1) < 10:
        d = '0' + str(end_date.month - 1)
    else:
        d = str(end_date.month - 1)

    e = str(end_date.day)
    f = str(end_date.year)
    g = freq

    yfURL = 'http://real-chart.finance.yahoo.com/table.csv?s='+s+'&a='+a+'&b='+b+'&c='+c+'&d='+d+'&e='+e+'&f='+f+'&g='+g+'&ignore=.csv'
    return yfURL

def main():
    webRequest = urllib2.Request(urlOfFile)
    try:
        page = urllib2.urlopen(webRequest)
        content = page.read()
        with open(filePath,'wb') as output:
            output.write(bytearray(content))   
    except urllib2.HTTPError as e:
        print e.fp.read()

    ticker = ['fb', 'c', 'goog']
    start_date = '2015-01-01'
    end_date = '2017-02-23'
    freq = 'd'
    localFilePath = '/Users/student/Downloads/datalibrary/'

    for x in ticker:
        yfURL = constructYFURL(x, start_date, end_date, freq)
        fileName = 'YF_' + x + '.csv'
        print yfURL    
        download(localFilePath + fileName, yfURL)

if __name__ == '__main__':
    main()