2017-07-07 51 views
0

我創建了用於從Mongodb導出csv的python腳本。它不能很好地工作,因爲它沒有從mongodb中導出所有數據。我使用get方法,因爲某些記錄在集合中被遺漏。python get方法不導出csv文件中的所有數據

MongoDB的樣本:

"experience": [ 
     { 
      "order": null, 
      "date": { 
       "to": ISODate("2017-07-04T21:24:31.872Z"), 
       "from": ISODate("2003-08-16T21:24:31.872Z") 
      }, 
      "description": "Trained horses for various competitions. ", 
      "company": "D2 Ranch", 
      "position": "Horse Trainer" 
     }, 
     { 
      "order": null, 
      "date": { 
       "to": ISODate("2017-07-04T21:24:31.872Z"), 
       "from": ISODate("2003-08-16T21:24:31.872Z") 
      }, 
      "description": "Trained horses for various competitions. 2", 
      "company": "D2 Ranch 2", 
      "position": "Horse Trainer 2" 
     } 
    ] 

的python腳本:

import sys 
sys.setdefaultencoding('utf-8') 
import codecs 
import csv 
cursor = db.user_profiles.find ({}, {'_id':1, 'experience.description':1, 'experience.position':1}) 
with codecs.open('skills.csv','w', encoding='utf-8') as outfile: 
    fields = ['_id', 'experience.description', 'experience.position']   
    write = csv.DictWriter(outfile, fieldnames=fields) 
    write.writeheader() 
    for x in cursor: 
    x_id = x['_id'] 
     for y in x.get('experience', {}):    
      z = { 
      '_id':x_id,       
      'experience.description':y.get('description',None), 
       'experience.position':y.get('position',None)}    
     write.writerow(z) 

的問題是,它跳過第二個記錄(位置: 「馬師2」)在體驗場。感謝您的幫助,謝謝

回答

0

它看起來像mongo查詢是好的,至少在版本3.4.4。它返回類似類型的字典的名單:

{ 
    "_id" : ObjectId("59995fb7513601164f1325f8"), 
    "experience" : [ 
     { 
      "description" : "Trained horses for various competitions. ", 
      "position" : "Horse Trainer" 
     }, 
     { 
      "description" : "Trained horses for various competitions. 2", 
      "position" : "Horse Trainer 2" 
     } 
    ] 
} 

看來,問題出在你的Python腳本(或者其格式,在蟒蛇什麼事情)。現在,在每個體驗列表中,此代碼僅存儲個元素。行寫代碼僅在for循環後運行。它應該在裏面,如下:

for y in x.get('experience', {}):    
    write.writerow({ 
     '_id':x_id,       
     'experience.description':y.get('description',None), 
     'experience.position':y.get('position',None) 
    })