如何使用python处理带有Mongodb的不规则json数据 10
1个回答
2017-08-24 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
#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
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
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询