2017-03-06 117 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