2017-03-06 111 views
0

我正在將一系列mongo集合導出到csv文件。我明確設置字段名稱通過調用字典導出,如果字段不存在於給定的文檔中,我仍然想在csv列中創建一個空字段Mongoexport空白字段csv

我目前正在收到這些錯誤在我的控制檯中: too many positional arguments: ['pfrm_uid', 'mark']]

這發生在所有集合上。我發現最奇怪的是在錯誤中有2個右括號。

for item_name in self.exported_fields_Dict.keys(): 

    fields_to_export = self.exported_fields_Dict[item_name] 
    self.item_count_dict[item_name] = self.item_count_dict[item_name] + 1 

    os.system("mongoexport --host localhost 
          --collection {0} 
          --db scrapy-mongodb-test 
          --type=csv 
          --out {1}.csv 
          --fields {2}".format(
            item_name, 
            item_name, 
            fields_to_export)) 

下面是我從

// collection name : fields_to_export 
exported_fields_Dict = { 
     'ent_pfrm': ['pfrm_uid', 'mark'], 
     'ent_indv': ['indv_uid', 'fName', 'lName', 'gender', 'DOB'], 
     'ent_meet': ['meet_uid', 'name', 'start_date', 'end_date'] 
    } 

拉動集合名稱和字段名字典任何提示/建議表示讚賞!

回答

1

mongoexport腳本需要用逗號分隔的字段作爲fields參數的字符串。

可以測試,看看這是不是這樣的字符串:

> print(exported_fields_Dict['ent_pfrm']) 
['pfrm_uid', 'mark'] 

您可以存儲字符串作爲你的字典值,例如"pfrm_uid, mark"或替代格式方法中的最後一個參數與

",".join(fields_to_export[item_name]) 

此打印你想要什麼:

+0

謝謝@thanasisp! – Chris