摘要:本文主要向大家介绍了Python语言生成内嵌式字典(dict)-案例从python提取内嵌json写入mongodb,通过具体的内容向大家展示,希望对大家学习Python语言有所帮助。
本文主要向大家介绍了Python语言生成内嵌式字典(dict)-案例从python提取内嵌json写入mongodb,通过具体的内容向大家展示,希望对大家学习Python语言有所帮助。
从mongo查询利用python 读写如新的集合import traceback,from gaode_hotel.conn_mongodb import conn_mongodbimport pandas as pdclass extra_yunnan_hotel(object):
def get_yunnan_hotel(self):
#查询
db_1 = conn_mongodb("gaode_pois","gaode_pois_hotel_beijing") #插入
db_2 = conn_mongodb("gaode_pois","gaode_pois_hotel_beijing_extra_mid010000")
data=db_1.db_find({}).limit(10) for x in data: #创建字典
dic={} #创建嵌套字典
dic.setdefault('geo',{})["loc"]={} try:
dic["_id"]=x["id"]
dic["name"]=x["name"] # dic["loction"]=x["location"]
dic.setdefault('geo',{})["loc"]=x["location"]
dic["lng"]=x["location"].split(',')[0]
dic["lat"]=x["location"].split(',')[1]
except:
print(traceback.print_exc())
start=extra_yunnan_hotel()
start.get_yunnan_hotel()结果成功: 更多的字典镶嵌 目的:生成{1: {‘errors’: {0: ‘d:\helloworld’, 1: ‘d:\dict’}, ‘os’: ‘windows’, ‘type’: 2}}这样的多层嵌套的数据。d1={} d1.setdefault(1,{})[errors]={}用同样的方式设置下一侧的嵌套temp = d1[1]
temp.setdefault(‘errors’,{})[error_len]=’d:\helloworld’
d1.setdefault(key,{})[‘os’]=’windows’
d1.setdefault(key,{})[‘type’]=2OK,这样就可以生成想要的格式的数据了。
本文由职坐标整理并发布,希望对同学们学习Python有所帮助,更多内容请关注职坐标编程语言Python频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号