遇到的问题,Mongodb 10万条的数据导入csv但是,总是只有5000条保存成功,其他数据都莫名其妙的丢失了;
import pymongo
import json
import csv
mongo_client = pymongo.MongoClient('172.22.32.8', 27017)
db = pymongo.database.Database(mongo_client, 'qi_resultdb')
result = db['all_city_lianjia'].find()
all_info = {}
num = 0
# data = ['id' ,'名称', '地址','建筑年代', '建筑类型' ,'开发商', '房屋总数', '楼栋总数', '物业公司', '物业费用', '附近门店']
#
# with open('链家.csv','a',encoding='utf-8') as f:
# writer = csv.writer(f)
# writer.writerow(data)
# all = []
with open('链家2.csv', 'w',encoding='utf-8',newline='') as f:
writer = csv.writer(f)
for row in result:
data = json.loads(row['result'])
num += 1
try:
li = [data['id'],data['名称'],data['地址'],data['建筑年代'],data['建筑类型'],
data['开发商'],data['房屋总数'],data['楼栋总数'],data['物业公司'],data['物业费用'],data['附近门店']]
except:
li = [data['id'],data['名称'],data['地址'],'',data['建筑类型'],
data['开发商'],data['房屋总数'],data['楼栋总数'],data['物业公司'],data['物业费用'],data['附近门店']]
finally:
import pymongo
import json
import csv
mongo_client = pymongo.MongoClient('172.22.32.8', 27017)
db = pymongo.database.Database(mongo_client, 'qi_resultdb')
result = db['all_city_lianjia'].find()
all_info = {}
num = 0
# data = ['id' ,'名称', '地址','建筑年代', '建筑类型' ,'开发商', '房屋总数', '楼栋总数', '物业公司', '物业费用', '附近门店']
#
# with open('链家.csv','a',encoding='utf-8') as f:
# writer = csv.writer(f)
# writer.writerow(data)
# all = []
with open('链家2.csv', 'w',encoding='utf-8',newline='') as f:
writer = csv.writer(f)
for row in result:
data = json.loads(row['result'])
num += 1
try:
li = [data['id'],data['名称'],data['地址'],data['建筑年代'],data['建筑类型'],
data['开发商'],data['房屋总数'],data['楼栋总数'],data['物业公司'],data['物业费用'],data['附近门店']]
except:
li = [data['id'],data['名称'],data['地址'],'',data['建筑类型'],
data['开发商'],data['房屋总数'],data['楼栋总数'],data['物业公司'],data['物业费用'],data['附近门店']]
# print(num)
#
finally:
print(li)#这里的数据是能够正常获取正常打印的,但是,写入scv的时候,数据会丢失,10万条数据,下来,最后只有5000多条数据写入成功了
writer.writerow(li)
print(num)
print('success')
finally:后面执行的代码,li这个变量的数据都是能够获取到的,但是,就是写入不成功,10万条数据,只有5000条数据写入成功,到目前为止,都没有找到合理的解释,逻辑上来说,是不应该的;解决办法就是删掉try,except,不用异常机制来做数据流程处理;