pymongo数据报表脚本

  最近打算将平时自己的一些小工具整理整理,由于一直使用python写脚本,而且数据库一直使用MongoDB,所以直接使用pymonogo驱动数据库做一些报表生成的小脚本,此次的脚本主要针对每个月中公司业务每天消耗的星星以及获取的星星数进行统计,其中需要用到的模块有pymongo、datetime。还是比较简单的。具体程序如下:

#! /usr/bin/env python
# coding = utf-8
import pymongo
from datetime import *

def connection():
  con = pymongo.Connection('192.168.1.242',5586)
  db = con.jykt
  return db

def starcount(beg_y,beg_m,beg_d,end_y,end_m,end_d,getstarfile,losestarfile):
  getfile = open('gsn.csv','w+')
  losefile = open('lsn.csv','w+')
  beginDay = datetime(int(beg_y),int(beg_m),int(beg_d))
  endDay = datetime(int(end_y),int(end_m),int(end_d)) + timedelta(days=1)
  tmp_beg = beginDay
  tmp_end = beginDay + timedelta(days=1)
  day_count = 0
  print '-------begin running--------'
  while tmp_end < endDay:
    getSum,loseSum = 0,0
    day_count += 1
    col = link.jyktCreditInfo.find({"createDate":{"$gte":tmp_beg,"$lte":tmp_end}})
    print '------running----------'
    print '******天次*******:%d' % day_count 
    for i in col:
      if i['credit'] > 0:
        getSum += i['credit']
      else:
        loseSum += i['credit']
    
    print "GetSum: %d" % getSum
    print "LoseSum: %d" % loseSum
    getfile.write(str(getSum)+'
')
    losefile.write(str(loseSum)+'
')   
    tmp_beg += timedelta(days=1)
    tmp_end += timedelta(days=1)
  getfile.close()
  losefile.close()

if __name__ == '__main__':
  a = raw_input("Input begin year:")
  b = raw_input("Input begin month:")
  c = raw_input("Input begin day:")
  d = raw_input("Input end year:")
  e = raw_input("Input end month:")
  f = raw_input("Input end day:")
  g = raw_input("Input getstarnumbers filename:")
  h = raw_input("Input losestarnumbers filename:")
  link = connection()
  starcount(a,b,c,d,e,f,g,h)

 还是比较简单的,没有考虑开销和性能,也没有使用一些python的高级处理,优化的东西还是比较多的。

与大家共勉!

原文地址:https://www.cnblogs.com/kirago/p/4369274.html