MongoDB - 将查询结果保存到excel文件中


import pymongo
import re

client = pymongo.MongoClient('127.0.0.1', 27017)
db_name = 'Trade'

db = client[db_name]


filter1 = {'$or':[{"phone": {'$ne':""}}, {"cell_phone": {'$ne':""}}]}
filter2 = {'address':1, 'contact':1, 'phone':1, 'cell_phone':1, '_id':0}

results = db.business.find(filter1, filter2)

from openpyxl import Workbook
wb = Workbook()

# grab the active worksheet
ws = wb.active

ws.append(('地址', '联系人', '电话', '手机'))
# for _ in a:
#     print(a.next())

for data in results:
    address = data['address']
    contact = data['contact']
    phone = data['phone']
    cell_phone = data['cell_phone']
    result = (address, contact, phone, cell_phone)
    ws.append(result)

wb.save("sample.xlsx")


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM