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 2.7 日期格式无法转换为数据格式_Python 2.7_Python 3.x - Fatal编程技术网

Python 2.7 日期格式无法转换为数据格式

Python 2.7 日期格式无法转换为数据格式,python-2.7,python-3.x,Python 2.7,Python 3.x,我正在尝试创建一个应用程序,它使用python从网站中提取信息,并且使用datetime格式 def constructYFURL(ticker,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=ticker.replace("^

我正在尝试创建一个应用程序,它使用python从网站中提取信息,并且使用
datetime
格式

def constructYFURL(ticker,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=ticker.replace("^","%5E")

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

    # Also the month always has 2 digits
    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)

    `enter code here`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


from download import constructYFURL
ticker = "AAPL"
start_date = "2016-01-01"
end_date = " 2017-03-06"
freq = "d"

yfURL = constructYFURL(ticker,start_date,end_date,freq)

print yfURL
def constructyfull(股票代码、开始日期、结束日期、频率):
开始日期=datetime.strtime(开始日期,%Y-%m-%d”).date()
end_date=datetime.strtime(end_date,“%Y-%m-%d”).date()
s=股票代码。替换(“^”和“%5E”)

如果开始日期为第1个月则在以下位置有额外的前导空格:

end_date = " 2017-03-06"
删除它,你得到的错误就会消失,程序就会运行

格式更好的代码:

def constructYFURL(ticker,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=ticker.replace("^","%5E")

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

    # Also the month always has 2 digits
    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


ticker = "AAPL"
start_date = "2016-01-01"
end_date = "2017-03-06"
freq = "d"

yfURL = constructYFURL(ticker,start_date,end_date,freq)

print yfURL
def constructyfull(股票代码、开始日期、结束日期、频率):
开始日期=datetime.strtime(开始日期,%Y-%m-%d”).date()
end_date=datetime.strtime(end_date,“%Y-%m-%d”).date()
s=股票代码。替换(“^”和“%5E”)
如果开始日期为第1个月