python操作Mongo数据库

时间:2022-09-16 15:48:33

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import operator
from pymongo import MongoClient
import sys
reload(sys)
sys.setdefaultencoding("utf-8")

DBC_W_RANK = MongoClient("192.168.1.10").game.weiboRank
DBC_W_ANALYSE = MongoClient("192.168.1.10").game.weiboAnalyse

def getZhangyixing():

print "名称,id,粉丝数,评论总量,转发总量,发博总量,点赞总量,平均评论量,平均转发量,平均发博量,平均点赞量\n"
for item in DBC_W_ANALYSE.find({"weiboId":"2706896955"}):
weiboId = str(item.get("weiboId"))
postStats = item.get("postStat")
leng = len(postStats)
totalC = 0
totalF = 0
totalP = 0
totalL = 0
for item in postStats:
totalC=item["commentCount"] + totalC
totalF = item["forwardCount"] + totalF
totalP = item["postCount"] + totalP
totalL = item["likeCount"] + totalL
avgC = totalC / float(leng)
avgF = totalF / float(leng)
avgP = totalP / float(leng)
avgL = totalL / float(leng)
string = "努力努力再努力x" + ","+str(weiboId)+","+" dd "+"," + str(totalC) + "," + str(totalF) + "," + str(totalP) + ","+ str(totalL)+ ","+ str(avgC) + "," + str(avgF) + "," + str(avgP)+ "," + str(avgL) + "\n"
print string
if __name__ == "__main__":
getZhangyixing()