簡體   English   中英

python get方法未導出csv文件中的所有數據

[英]python get method not exporting all data in csv file

我創建了從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”)。 感謝您的幫助,謝謝

看起來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)
    })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM