Python访问MongoDB,并且转换成Dataframe,,#!/usr/bin
Python访问MongoDB,并且转换成Dataframe,,#!/usr/bin
#!/usr/bin/env python# -*- coding: utf-8 -*-# @Time : 2018/7/13 11:10# @Author : baoshan# @Site : # @File : pandans_pymongo.py# @Software: PyCharm Community Editionimport pymongoimport pandas as pddef _connect_mongo(host, port, username, password, db): """ A util for making a connection to mongo. """ if username and password: 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) 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 dfread_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. 解决了中文乱码问题。
谢谢
【参考】https://stackoverflow.com/questions/16249736/how-to-import-data-from-mongodb-to-pandas
Python访问MongoDB,并且转换成Dataframe
相关内容
- python-flask-scoped_session创建session的两种方式,,scoped_ses
- 吴裕雄 python 神经网络——TensorFlow 图像预处理完整样例
- python 字符串转列表出现\ufeff的解决方法,, 在学习
- python-selenium -- iframe及滚动条定位方法详解,,一、fram
- Python 代码整洁优化,,1.提炼函数# 提炼
- Python3之harbor sdk api,,环境python3源
- python 2.6涓?No module named sysconfig,,鏍囩锛?a hre
- python -u,,标准错误(std.e
- python xlsxwriter写excel并操作各种格式属性,, 1 # -*- c
- python2 和 python3兼容写法,,一:使用Import
评论关闭