您好,登錄后才能下訂單哦!
如下所示:
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/7/13 11:10 # @Author : baoshan # @Site : # @File : pandans_pymongo.py # @Software: PyCharm Community Edition import pymongo import pandas as pd def _connect_mongo(host, port, username, password, db): """ A util for making a connection to mongo. """ if username and password: mongo_uri = "mongodb://%s:%s@%s:%s/%s" % (username, password, host, port, db) conn = pymongo.MongoClient(mongo_uri) else: conn = pymongo.MongoClient(host, port) return conn[db] def read_mongo(db, collection, query={}, host='test43', port=27017, username=None, password=None, no_id=True): """ Read from Mongo and Store into DataFrame. """ # Connect to MongoDB db = _connect_mongo(host=host, port=port, username=username, password=password, db=db) # Make a query to the specific DB and Collection cursor = db[collection].find(query).limit(10) # Expand the cursor and construct the DataFrame df = pd.DataFrame(list(cursor)) df.to_csv("abc.csv", encoding="utf_8_sig") # 處理中文亂碼問題 if no_id: del df['_id'] return df read_mongo(db='service', collection='trace_log_regular', query={}, host='xxx', port=27017, username="xxx", password="xxx")
小結:
1. 解決了pymongo訪問MongoDB的問題
2. 解決了查詢的數據轉成dataframe的問題
3. 解決了dataframe寫入到csv的問題
4. 解決了中文亂碼問題。
以上這篇Python訪問MongoDB,并且轉換成Dataframe的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。