2016-10-25 65 views
1

我在从mongodb将数据导出到csv时,遇到python脚本中缺少字段名称的问题。类型字段名称存在于第一条记录中,但它不会出现在其余记录中。如果编写python脚本,如果它不存在,则为类型字段提供空值。从mongodb导出json数据到csv

的MongoDB集合的样本:

"stages": [ 
    { 
     "interview": false, 
     "hmNotification": false, 
     "hmStage": false, 
     "type": "new", 
     "isEditable": false, 
     "order": 0, 
     "name": { 
      "en": "New" 
     }, 
     "stageId": "51d1a2f4c0d9887b214f3694" 
    }, 
    { 
     "interview": false, 
     "hmNotification": true, 
     "isEditable": true, 
     "order": 1, 
     "hmStage": true, 
     "name": { 
      "en": "Pre-Screen" 
     }, 
     "stageId": "51f0078d7297363f62059699" 
    }, 
    { 
     "interview": false, 
     "hmNotification": false, 
     "hmStage": false, 
     "isEditable": true, 
     "order": 2, 
     "name": { 
      "en": "Phone Screen" 
     }, 
     "stageId": "51d1a326c0d9887721778eae" 
    }] 

Python脚本的示例:

import csv 
cursor = db.workflows.find({}, {'_id': 1, 'stages.interview': 1, 'stages.hmNotification': 1, 'stages.hmStage': 1, 'stages.type':1, 'stages.isEditable':1, 'stages.order':1, 
'stages.name':1, 'stages.stageId':1 }) 
flattened_records = [] 
for stages_record in cursor: 
    stages_record_id = stages_record['_id'] 
    for stage_record in stages_record['stages']: 
     flattened_record = { 
      '_id': stages_record_id, 
      'stages.interview': stage_record['interview'], 
      'stages.hmNotification': stage_record['hmNotification'], 
      'stages.hmStage': stage_record['hmStage'], 
      'stages.type': stage_record['type'], 
      'stages.isEditable': stage_record['isEditable'], 
      'stages.order': stage_record['order'], 
      'stages.name': stage_record['name'], 
      'stages.stageId': stage_record['stageId']}     
     flattened_records.append(flattened_record) 

运行python脚本时,它显示KeyError异常: “类型”。请帮助我如何在脚本中添加缺少的字段名称。

回答

0

当您尝试提取可能不存在于Python字典中的值时,可以使用dict类的.get()方法。

举例来说,假设你有一个这样的词典:

my_dict = {'a': 1, 
      'b': 2, 
      'c': 3} 

可以使用get方法来获取生存的关键之一:

>>> print(my_dict.get('a')) 
1 

但是,如果你尝试得到一个不存在的密钥(如does_not_exist),默认情况下您将得到None

>>> print(my_dict.get("does_not_exist")) 
None 

如文档中提到的,你也可以提供,当该键不存在,将返回一个默认值:

>>> print(my_dict.get("does_not_exist", "default_value")) 
default_value 

但如果键确实存在此默认值将不会被使用在字典(如果该键不存在,你会得到它的值):

>>> print(my_dict.get("a", "default_value")) 
1 

知道,当你建立你的flattened_record你可以这样做:

'stages.hmStage': stage_record['hmStage'], 
'stages.type': stage_record.get('type', ""), 
'stages.isEditable': stage_record['isEditable'], 

因此,如果stage_record字典不包含密钥type,get('type')将返回一个空字符串。

您也可以尝试只用:

'stages.hmStage': stage_record['hmStage'], 
'stages.type': stage_record.get('type'), 
'stages.isEditable': stage_record['isEditable'], 

然后stage_record.get('type')将返回Nonestage_record不包含type关键。

或者你可以使默认"UNKNOWN"

'stages.type': stage_record.get('type', "UNKNOWN"), 
+1

它工作时,增加了“stages.typ非常好‘:stage_record.get。在python脚本(’类型”)类型的值出现在CSV文件。非常感谢。 – user7070824

+0

感谢您的帮助。如何删除u字母,括号和Python中子字段的名称?谢谢。 – user7070824