圆月山庄资源网 Design By www.vgjia.com

本文实例讲述了python读取json文件并将数据插入到mongodb的方法。分享给大家供大家参考。具体实现方法如下:

#coding=utf-8
import sunburnt
import urllib
from pymongo import Connection
from bson.objectid import ObjectId
import logging
from datetime import datetime
import json
from time import mktime
from feedparser import _parse_date as parse_date
import time
import sys
import getopt
import ConfigParser
args = sys.argv[1:]
optlist, args = getopt.getopt(args, 'c:')
cmd_opt = {}
for opt in optlist:
  cmd_opt[opt[0]] = opt[1]
conf_file = cmd_opt['-c']
config = ConfigParser.ConfigParser()
config.read(conf_file)
hostname = config.get("mongodb", "hostname")
port_num = int(config.get("mongodb", "port_num"))
db_name = config.get("mongodb", "db")
connection = Connection(hostname, port_num)
db = connection[db_name]
courseTable = db.course
lecTable = db.lecture
try:
  f = file("json1-14/14.json")
  s = json.load(f)
  courseData = s["results"]["course"]
  lecDataArr = s["results"]["lecture"]
  f.close
  print "get file content successfully!"
  #insert course
  courseId = courseTable.save(courseData)
  courseId = str(courseId)
  print "courseId: "+courseId
  print "lec length: "+str(len(lecDataArr))
  #insert lecture
  lecIdArr = []
  for lecData in lecDataArr:
    lecData["course_id"] = courseId
    lecId = lecTable.save(lecData)
    lecIdArr.append(str(lecId))
  # update course
  courseTable.update({'_id':ObjectId(courseId)},
            {"$set":{"lectures.lecture_id_list":lecIdArr}},
            upsert=True, multi=True);
  print 'insert successfully!'
except Exception, e:
  print e

希望本文所述对大家的Python程序设计有所帮助。

标签:
python,读取,json文件,数据,插入,mongodb,方法

圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com