#!/usr/bin/env python#-*-coding:utf-8-*-#@Time: 2018/7/13 11:10#@Author: Baoshan#@Site:#@File: pandans_pymongo.py#@Software: pycharm Community EditionImportPymongoImportPandas as PDdef_connect_mongo (host, port, username, password, db):"""a util for making a connection to MONGO.""" ifUsername andPassword:mongo_uri="Mongodb://%s:%[email protected]%s:%s/%s"%(username, password, host, port, DB) Conn=Pymongo. Mongoclient (Mongo_uri)Else: Conn=Pymongo. Mongoclient (host, Port)returnConn[db]defRead_mongo (DB, Collection, query={}, host='test43', port=27017, Username=none, Password=none, no_id=True):"""Read from Mongo and Store into DataFrame.""" #Connect to MongoDBdb = _connect_mongo (Host=host, Port=port, Username=username, Password=password, db=db)#Make a query to the specific DB and Collectioncursor = db[collection].find (query). Limit (10) #Expand the cursor and construct the DataFrameDF =PD. DataFrame (cursor) df.to_csv ("Abc.csv", encoding="Utf_8_sig")#handling Chinese garbled problems ifno_id:deldf['_id'] returnDfread_mongo (DB='Service', collection='Trace_log_regular', query={}, host='XXX', port=27017, Username="XXX", password="XXX")
Summary:
1. Fixed Pymongo access to MongoDB
2. Solve the problem of turning query data into Dataframe
3. Resolves an issue with dataframe writing to CSV
4. Solved the problem of Chinese garbled characters.
Thank you
"Reference" Https://stackoverflow.com/questions/16249736/how-to-import-data-from-mongodb-to-pandas
Python accesses MongoDB and converts to Dataframe