I want to use a for loop to print every date between 2 dates. Actually going to merge this with a MySQL query to pass the date into the query itself and into the filename of the output.
So, how can I change this:
sum = 0
for i in range(1,11):
print sum
sum += i
To this?
InputDate = '2009-01-01'
for i in range('2009-01-01','2009-07-01'):
print InputDate
InputDate += i
I realize there is something in rrule that does this exact function:
a = date(2009, 1, 1)
b = date(2009, 7, 1)
for dt in rrule(DAILY, dtstart=a, until=b):
print dt.strftime("%Y-%m-%d")
But, I am restricted to older version of python.
This is the shell script version of what I am trying to do, if this helps clarify:
while [InputDate <= EndDate]
do
sql="SELECT Date,SUM(CostUsd) FROM DailyStats WHERE Date = '$InputDate' GROUP BY Date"
name=$(mysql -h -sN -u -p -e "$sql" > DateLoop-$InputDate.txt db)
echo "$name"
InputDate=$(( InputDate + 1 ))
done
So how can I do this in Python?
Adding follow up question here for readability. Unfortunately I can not use standard MySQL library as we have a proprietary setup with numerous instances running in parallel. The only way to run this type of query is to connect to one instance at a time, on the command line.
while day <= b: print "Running extract for :" day
sql="SELECT Date,SUM(CostUsd) FROM Stats d WHERE d.Date = " + day + " GROUP BY Date"
os.system('mysql -h -sN -u -p -e " + sql + " > DateLoop-" + day + ".txt db')
day += one_day