日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python批量导入MongoDB数据库

發布時間:2025/3/8 python 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python批量导入MongoDB数据库 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

由于源文件不是MongoDB支持的JSON和BSON 格式的數據, 所以只能將源數據轉變格式后, 用腳本將其導入,所測數據為中科院信工所提供的數據。(這也是在信工所接觸的第一個寫程序的活。)源碼如下:

#!/usr/bin/env python #encoding:utf-8 ################################### # function: analysis the log in the 'die' to JSON , and #output the data to the MongoDB. # data: 2014/3/31 #History: 1.0 ################################### import os import pymongo def connect_mongodb(): servers="mongodb://localhost:27017" conn = pymongo.Connection(servers) print conn.database_names() db = conn.my_mongodb #連接庫 return db def str_process(string,db): d={} if string == '\n': return string2=str(string) print '-----'+string string2=string2.split(' ') print '---------------' print string2 for i in string2: print i print '------------' string2[3].split('\n') d['projectcode']=string2[0] d['pagename']=string2[1] d['pageview']=string2[2] d['bytes']=string2[3][:-1] db.user.insert(d) def file_process(source_file,db): string2='' f=open(source_file,'r') print 'file name :'+source_file while True: string2=f.readline() if string2 == '': break string2=str_process(string2,db) print string2 def get_dir_list(dir): #input the dir ,will output the all filename dat0=[] for i in os.listdir(dir): dat0.append(i) return dat0 def all_file_process(): dir_file_name='' dir_list=[] dir_file_name=raw_input('please input the dir name:') dir_list=get_dir_list(dir_file_name) print dir_list db=connect_mongodb() for i in dir_list: if str(i) != 'log_file_process.py': file_process(str(i),db) all_file_process()

最新內容請見作者的GitHub頁:http://qaseven.github.io/

總結

以上是生活随笔為你收集整理的python批量导入MongoDB数据库的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。