码迷,mamicode.com
首页 > 数据库 > 详细

python 导出mongoDB数据中的数据

时间:2017-09-03 09:58:33      阅读:550      评论:0      收藏:0      [点我收藏+]

标签:orm   default   history   name   ftime   imp   url   mongo   127.0.0.1   

import pymongo,urllib
import sys
import time
import datetime
reload(sys)
sys.setdefaultencoding(‘utf8‘)
from pymongo.connection import Connection
from datetime import timedelta
import os

connection=pymongo.Connection(‘127.0.0.1‘,27017)
db = connection.upload
db.authenticate("username","passowd")
format="%Y-%m-%d %H:%M:%S"
fileName="dstUrl"
datefmt="%m%d"

now = datetime.datetime.now().replace(day=1,hour=0,minute=0,second=0)
aDay = timedelta(days =-1)
endDate=now+aDay
startDate=endDate.replace(day=1)
endDateStr=endDate.strftime(format)
startDateStr=startDate.strftime(format)
startTime=int(time.mktime(time.strptime(startDateStr, ‘%Y-%m-%d %H:%M:%S‘))*1000)
endTime=int(time.mktime(time.strptime(endDateStr, ‘%Y-%m-%d %H:%M:%S‘))*1000)
#f=open(fileName, "w")
fileName=fileName+"("+startDate.strftime(datefmt)+"-"+endDate.strftime(datefmt)+").txt"

def findList(startTime,endTime):
print "startTime"+startDateStr+" endTime"+endDateStr
query={"completeTime":{"$gte":startTime,"$lt":endTime},"status":30,"videoCDNStatus":10,"checksumStatus":10,"mmsStatus":1}
encodeCursor=db.video_encode.find(query)
historyCursor=db.video_encode_history.find(query)
taskIterator(encodeCursor)
taskIterator(historyCursor)

def taskIterator(cusor):
for encode in cusor:
mid=encode["mid"]
encodeId=encode["encodeId"]
vtype=encode["vtype"]
dstUrl=encode["dstUrl"]
checksumPath=encode["checksumPtah"]
f.write(str(mid)+","+str(encodeId)+","+vtype+","+dstUrl+","+checksumPath+"\n")

print "start run to export dst url to file %s",fileName
if(os.path.isfile(fileName)):
print "file %s is exsis ,not create file",fileName
else:
f=open(fileName, "w")
findList(startTime,endTime)
f.close;

python 导出mongoDB数据中的数据

标签:orm   default   history   name   ftime   imp   url   mongo   127.0.0.1   

原文地址:http://www.cnblogs.com/adolfmc/p/7468631.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!