zoukankan      html  css  js  c++  java
  • python 导出mongoDB数据中的数据

    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+" ")

    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;

  • 相关阅读:
    CentOS创建vsftp进行读写操作的简单方法
    [知乎]经典古诗词
    js仿手机端九宫格登录功能
    Android Studio 小技巧合集
    Android MVPR 架构模式
    剖析OkHttp缓存机制
    Java Observable 模式
    使用Rxjava缓存请求
    ThreadPoolExecutor使用和思考(上)-线程池大小设置与BlockingQueue的三种实现区别
    ThreadPoolExecutor使用介绍
  • 原文地址:https://www.cnblogs.com/adolfmc/p/7468631.html
Copyright © 2011-2022 走看看