[置顶] python 生成内嵌式字典(dict)-案例从python提取内嵌json写入mongodb
2017-12-22 16:25
761 查看
从mongo查询利用python 读写如新的集合
结果成功:
更多的字典镶嵌
目的:
用同样的方式设置下一侧的嵌套
OK,这样就可以生成想要的格式的数据了。
import traceback, from gaode_hotel.conn_mongodb import conn_mongodb import pandas as pd class 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 = d d3ae 1[1] temp.setdefault(‘errors’,{})[error_len]=’d:\helloworld’ d1.setdefault(key,{})[‘os’]=’windows’ d1.setdefault(key,{})[‘type’]=2
OK,这样就可以生成想要的格式的数据了。
相关文章推荐
- python(dict字典相关知识以及小例子:生成一个列表,存放100个随机整数,找出出现次数最多的数字)
- Python 提取dict转换为xml/json/table并输出
- [置顶] 无限遍历,python实现在多维嵌套字典、列表、元组的JSON中获取数据
- Python3基础 dict 推导式 生成10以内+奇数的值为True 偶数为False的字典
- python 读取单所有json数据写入mongodb(单个)
- Python 提取dict转换为xml/json/table并输出的实现代码
- Python读取json文件,并转化为字典进行提取字段(出现索引must be int,not str)解决方案
- Python连接mongodb提取部分字段内数据并写入txt文件
- Python txt文件读取写入字典的方法(json、eval)
- Python 提取dict转换为xml/json/table并输出的实现代码
- [置顶] 【python 数据库写入】python把数据框数据写入mongodb、mysql
- 详解python中的json和字典dict
- Python3 初学实践案例(10)对象转字典 object to dict
- python 使用 simplejson 将字符串转换成字典dict
- Python读取Json字典写入Excel表格的方法
- mongodb取出json,利用python转成dataframe(dict-to-dataframe)
- Python2.7 写入JSON文件,无法显示中文
- kettle案例三抽取json数据保存到mongodb
- python3基础: 元组tuple、 列表list、 字典dict、集合set。 迭代器、生成器
- python使用pickle,json等序列化dict