In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces how to use the mongodb module in Python, the content is very detailed, interested friends can refer to, hope to be helpful to you.
When it comes to python and mongodb, then installing the corresponding modules is essential, the easiest way to install, or it must be pip.
# pip install pymongo==3.0.4
By the way, record how the source code is installed.
# wget https://pypi.python.org/packages/source/p/pymongo/pymongo-2.8.tar.gz#md5=23100361c9af1904eb2d7722f2658114-- no-check-certificate# tar xf pymongo-2.8.tar.gz# cd pymongo-2.8# python setup.py install
Taken from a log.
35783 s100 android 47 5 192.168.1.100 2015-09-05 08:03:19 strengthenHeroByHeroes {"consume_gold": {"ogold": 2893821, "cgold": 1700, "gold": 2892121, "tag": "strengthenHeroByHeroes"}, "taskInfo": [{"id": 2310033, "progress": 2, "status": 0}], "delHeroList": {"id": 102014, "id": 102010, "id": 102010}, "id": 100026 "olevel": 46, "oexp": 1700, "cexp": 1700, "level": 46, "exp": 3400} 865982021462182 XiaoMi
Python script for storing mongodb
[root@localhost opt] # cat analytical.py #! / usr/bin/env python#coding:utf8import os,sys,jsonfrom datetime import * from pymongo import MongoClientdef ConMongo (host,port,cur_db,username,password): client = MongoClient (host,port) db = client [cur _ db] db.authenticate (username,password) table = db.gamelogs return tabledef parseLog (logfile) Table): dic = {} dl = [] with open (file_log) as fd: for line in fd: try: tokens = line.strip () .split ('\ t') uid = tokens [0] server = tokens [1] system = tokens [2] level = int (tokens [3]) vip_level = tokens [4] ip = tokens [5] time = datetime.strptime (tokens [6] "% Y-%m-%d% H:%M:%S") # convert the time string to the time format action = tokens [7] result = json.loads (tokens [8]) # Special string to json format uuid = tokens [ 9] if len (tokens) = = 12: channel = tokens [11] else: channel =''dic = {' uid':uid 'server':server,'system':system,'level':level,'vip_level':vip_level,'ip':ip,'time':time,'action':action,'result':result,'uuid':uuid 'channel':channel} dl.append (dic) if len (dl) = = 10000: table.insert_many (dl) dl = [] except Exception,e: print e Line if len (dl) > 0: table.insert_many (dl) if _ _ name__ ='_ _ main__': table = ConMongo ('localhost',27017,'talefundb','talefun','123456') try: logfile = sys.argv [1] parseLog (logfile,table) except IndexError,e: print e
Note:
(1) the insert_many parameter is added in mongodb 3.0.4, which allows you to insert a large list directly into the mongodb database. (2) the script makes restrictions. If there are 2000 values in the dictionary, insert the data once into mongodb. This ensures the efficiency. (3) it is not recommended to copy the script test directly. After many tabs are pasted out, problems will occur. I will put the script on the cloud disk and you can download it and test it.
On how to use the mongodb module in Python to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.