python get方法不导出csv文件中的所有数据

时间:2017-07-07 17:51:33

标签: python mongodb csv

我创建了用于从Mongodb导出csv的python脚本。它没有很好地工作,因为它没有从mongodb导出字段中的所有数据。我使用get方法,因为在集合中遗漏了一些记录。

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)
问题是它在经验领域中跳过第二条记录(位置:“Horse Trainer 2”)。感谢任何帮助,谢谢

1 个答案:

答案 0 :(得分: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脚本(或它的格式,在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)
    })