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」)在體驗場。感謝您的幫助,謝謝