romeiovasu
asked on
error with datetime python
here is my code can some one help me with the error please.
from download import constructYFURL
ticker = "AAPL"
start_date = "2016-01-01"
end_date = " 2017-03-06"
freq = "d"
yfURL = constructYFURL(ticker,star t_date,end _date,freq )
print yfURL
i am getting an error saying
ValueError: time data ' 2017-03-06' does not match format '%Y-%m-%d'
here is the code for import constructYFURL
from datetime import datetime
def constructYFURL(ticker,star t_date,end _date,freq ):
start_date = datetime.strptime(start_da te,"%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)
# a represents the month portion - however the month count starts from 0
# Also the month always has 2 digits
b=str(start_date.day)
c=str(start_date.year)
# b and c represent the day and year parts of the start date
if end_date.month - 1 < 10:
d = "0" + str(end_date.month - 1)
else:
d = str(end_date.month - 1)
# similarly we have to set up the month part for the end date
e=str(end_date.day)
f=str(end_date.year)
# e and f represent the day and year parts of the end date
g=freq
# g represents the frequency d = daily, w= weekly, m=monthly
# Finally let's set up the URL
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+"&ign ore=.csv"
return yfURL
from download import constructYFURL
ticker = "AAPL"
start_date = "2016-01-01"
end_date = " 2017-03-06"
freq = "d"
yfURL = constructYFURL(ticker,star
print yfURL
i am getting an error saying
ValueError: time data ' 2017-03-06' does not match format '%Y-%m-%d'
here is the code for import constructYFURL
from datetime import datetime
def constructYFURL(ticker,star
start_date = datetime.strptime(start_da
end_date = datetime.strptime(end_date
s=ticker.replace("^","%5E"
if start_date.month-1<10:
a="0"+str(start_date.month
else:
a=str(start_date.month-1)
# a represents the month portion - however the month count starts from 0
# Also the month always has 2 digits
b=str(start_date.day)
c=str(start_date.year)
# b and c represent the day and year parts of the start date
if end_date.month - 1 < 10:
d = "0" + str(end_date.month - 1)
else:
d = str(end_date.month - 1)
# similarly we have to set up the month part for the end date
e=str(end_date.day)
f=str(end_date.year)
# e and f represent the day and year parts of the end date
g=freq
# g represents the frequency d = daily, w= weekly, m=monthly
# Finally let's set up the URL
yfURL = "http://real-chart.finance.yahoo.com/table.csv?s="+s+"&a="+a+"&b="+b+"&c="
return yfURL
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.