mongo 取随机100条数据写入Excel
Posted zhaoyingjie
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mongo 取随机100条数据写入Excel相关的知识,希望对你有一定的参考价值。
Python 读取Mongo取随机100条数据写入Excel
# 随机100人获取 关卡数、现金账户、金币账户 from models import UserDayStat, GameUser, UserInfo import xlwt book = xlwt.Workbook() def write_xls(data, sheet_name, excel_name, title_list): global book sheet = book.add_sheet(sheet_name) # 添加一个sheet页 # 写入标题 for i, v in enumerate(title_list): sheet.write(0, i, v) row = 0 for stu in data: col = 1 # 从第二行写入 第一行为标题 for s in stu: # 再循环里面list的值,每一列 sheet.write(col, row, s) col += 1 row += 1 book.save("{}.xls".format(excel_name)) # 保存到当前目录下 return book if __name__ == ‘__main__‘: device_id_list = [] pipeline = [ {‘$match‘: {‘date‘: {‘$gte‘: "2019-12-20", ‘$lte‘: "2019-12-29"}}}, {‘$project‘: {‘_id‘: 0, ‘device_id‘: 1}}, {‘$sample‘: {‘size‘: 100}} ] # device_id 列表 userdaystat = UserDayStat.objects().aggregate(*pipeline) for user in userdaystat: device_id_list.append(user.get("device_id")) # 关卡列表 guanka_list = [] for divice_id in device_id_list: gameuser = GameUser.objects.get(device_id=divice_id) guanka_list.append(gameuser.game_level) # 金币账户&&现金账户列表 coin_balance_list = [] cash_list = [] for divice_id in device_id_list: userinfo = UserInfo.objects.get(device_id=divice_id) coin_balance_list.append(userinfo.cash) cash_list.append(userinfo.coin_balance) all_data = [] all_data.append(device_id_list) all_data.append(guanka_list) all_data.append(coin_balance_list) all_data.append(cash_list) title_list = ["device_id", "关卡数", "现金账户", "金币账户"] write_xls(data=all_data, sheet_name=‘随机1100人‘, excel_name="随人b", title_list=title_list)
以上是关于mongo 取随机100条数据写入Excel的主要内容,如果未能解决你的问题,请参考以下文章