- #数据抽取
-
sql = """
-
select
-
DATE_FORMAT(time,'%Y-%m-%d') as day,
-
DATE_FORMAT(time,'%H:%i') as time ,
-
pv
-
from
-
minute_stat
-
"""
-
-
sql += """
-
where
-
time >= '%s' and
-
time <='%s' and
-
type=%s and id=%s
-
""" % (start_time,end_time,ctype,cid)
-
-
mycursor.execute(sql)
-
datas={}
-
for day,time,pv in mycursor.fetchall() :
-
if not datas.has_key(day) : datas[day]={}
-
if not datas[day].has_key(time) : datas[day][time]=''
-
datas[day][time]=pv
-
-
input=open('/home/skynet/public/coding/R/3d_pv/data.txt','w')
-
times = utils.getMin5s('2011-01-01 00:00','2011-01-01 23:59','%H:%M')
-
-
#print>>input,"\t"+"\t".join(times)
-
for day in utils.getdiffDays(start_time,end_time):
-
#row = [day]
-
row = []
-
for te in times :
-
if datas.has_key(day) and datas[day].has_key(te):
-
row.append( datas[day][te] )
-
else:
-
row.append(0)
-
print len(row)
-
print>>input,"\t".join([ str(c) for c in row ] )
-
input.close()
#获取两个时间段的所有时间,返回list
def getdiffDays(beginDate,endDate,format='%Y-%m-%d'):
tformat="%Y-%m-%d";
bd=str2date(beginDate,tformat)
ed=str2date(endDate,tformat)
oneday=datetime.timedelta(days=1)
num=daydiff(beginDate,endDate)+1
li=[]
for i in range(0,num):
li.append( date2str(ed,format) )
ed=ed-oneday
return li[::-1]
#两个日期相隔多少天,例:2008-10-03和2008-10-01是相隔两天
def daydiff(beginDate,endDate):
format="%Y-%m-%d";
bd=str2date(beginDate,format)
ed=str2date(endDate,format)
oneday=datetime.timedelta(days=1)
count=0
while bd!=ed:
ed=ed - oneday
count+=1
return count